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?

Community
  • 1
  • 1
Charanoglu
  • 1,229
  • 2
  • 11
  • 30

1 Answers1

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
…
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 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! – Charanoglu May 24 '18 at 17:14
  • you could do it in systemd+syslog: https://stackoverflow.com/questions/37585758/how-to-redirect-output-of-systemd-service-to-a-file – Martin Ullrich May 24 '18 at 17:45
  • 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) – Martin Ullrich May 24 '18 at 17:46
  • wait, I mean the output generated by console.writeline not the log.txt – Charanoglu May 24 '18 at 17:48