2

I developed a simple application using NetCore, the app write the content on a file every time is executed, this is the code:

using System;
using System.IO;

namespace SimpleApp
{
   class Program
   {
       static void Main(string[] args)
       {
          using (StreamWriter writer = new StreamWriter("log.txt", true))
          {
              writer.WriteLine("Hello World");
          }
       }
   }
}

so if I start the application in this way: dotnet SimpleApp.dll I get a log.txt file with Hello World in it.

Now, I'm trying to create a linux daemon, I have no experience in that, so I wrote what I learned on Internet, I created a service cakked console.service which contains this structure:

[Unit]
Description = Hello World Daemon

[Service]
ExecStart = /usr/bin/dotnet /home/my username/Desktop/publish/SimpleApp.dll
Restart = on-failure

[Install]
WantedBy = multi-user.target

So essentially I have a description, and I setted in ExecStart the path of my dotnet installation and the path of the application.

Later I have an Install that if understood well tell to the systemctl that the service can run for each user, right?

Later, I copied the service in the system folder:

sudo cp console.service /lib/systemd/system

and I enabled it:

sudo systemctl daemon-reload 
sudo systemctl enable console.service

so, I executed the service:

sudo systemctl start console.service

and when I print the status:

systemctl status console.service

will is displayed this:

enter image description here

the problem's that inside the folder publish (the path of the application specified in ExecStart) I doesn't have any log.txt at this time.

Why?

1 Answer 1

2

Since you only specified a relative path for the file, it will be created in the working directory of the service.

You can either change "log.txt" to a full path or set the working directory in the .service file:

[Service]
WorkingDirectory=/path/you/want/the/file/to/be/in
…
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, just a little question: suppose that I want display the Console.WriteLine as a "terminal log", is possible to do this? Should I write some configuration in the service file or I should edit my c# console? Thanks for the help!
you could do it in systemd+syslog: stackoverflow.com/questions/37585758/…
or use any .net logging library (e.g. serilog with a rolling log file sink) (Microsoft.Extensions.Logging acts as abstraction layer if you use libraries or want to create independent components)
wait, I mean the output generated by console.writeline not the log.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.