Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog in Windows-Service not writing to logfile

I am using Serilog within an TopShelf Service, logging to the console and a rolling file. When running the service in a console my messages are written to the logfile, but when I install the service and run it no logging occurs. Is there anything special I need to configure? The file is written to the binaries folder under ".\logs\log-{date}.txt".

like image 323
Gope Avatar asked Oct 26 '14 10:10

Gope


People also ask

How do I enable Serilog?

Structured Logging with Serilog. To enable structured logging with the File Sink, we need to add a JSON formatter as a Parameter to the Settings. Let's add new Sink to our appSettings. json/Serilog. Add the below code as the sink.

How do you use Serilog logging?

Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.

How do you test Serilog?

From the official site:https://serilog.net/, "Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent . NET platforms. Unlike other logging libraries, Serilog is built with powerful structured event data in mind."

What is Serilog sink?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.


5 Answers

I had a very similar issue. In my case, the problem was with relative paths. I just had to specify the absolute path. Now it works like a charm.

WriteTo.RollingFile(AppDomain.CurrentDomain.BaseDirectory + "\\logs\\log-{Date}.log")
like image 116
Michael Avatar answered Oct 08 '22 22:10

Michael


We had the same issue, this is what we found:

  • Serilog configured for rolling logfiles and writing to Seq
  • Ddrable logs was enabled.

Symptoms - No log files were being created - No logs written to Seq.

As per @gdoten's comment, our log files were being written to \windows\syswow64 (service was running as localservice).
We believe the permissions on these files may not of allowed the durable spool file to be read causing no logs to be written to Seq.

Fix was to hard code the path of the rollinglogfile and buffer.

like image 38
Jafin Avatar answered Oct 09 '22 00:10

Jafin


 loggerFactory.AddFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log-{Date}.txt"));

Try this one, it worked in my ASP.NET core 2 application runs as windows service

like image 23
Grigor Yeghiazaryan Avatar answered Oct 08 '22 23:10

Grigor Yeghiazaryan


It's most likely that the account under which the services is running lacks permission to write to the log file location. Try changing the log file location to the system's temp folder to see if this is the case.

If this still fails, using Serilog's SelfLog to get exception information is your best bet.

like image 28
Nicholas Blumhardt Avatar answered Oct 08 '22 22:10

Nicholas Blumhardt


I had a similar issue, it ends up because the following code,

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

the code specify the appsettings.json file, and in that file it has the serilog's configuration setttings, but when run in service, the current folder is not point to the executable file folder. so it can not find the file appsettings.json, and then of course serilog wont work as expected.

just need to change the code as following will make it work

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
like image 29
liuhongbo Avatar answered Oct 08 '22 23:10

liuhongbo