5

I have a Windows Service that processes long running tasks and never shuts down unless forcefully done so (i.e. computer shutdown). I would like to keep the same Serilog instance alive the whole time Windows service is up and running (for performance reason).

However, I only see logs after the Dispose() call as shown below.

var logger = new LoggerConfiguration()
            .WriteTo.Seq("http://localhost:5341")
            .CreateLogger();

logger.ForContext("TestContext", new { Machine = "TSTDB2" }, true).Information("Test");

logger.Dispose();

I would not mind a few seconds delay but having to wait until the Serilog instance is disposed does not work for me. Any solutions would be greatly appreciated.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
SamDevx
  • 2,268
  • 4
  • 32
  • 47
  • Is the process exiting in your test, or have you tried it in the long-running process? Any chance you can reconstruct your scenario in a minimal `Program.cs` reproduction? Are you creating more than one logger instance, or assigning the logger to a static field somewhere? (HTH, just trying to generate ideas.) – Nicholas Blumhardt Oct 27 '17 at 03:03

2 Answers2

1

By default, the Seq Sink waits for 2 seconds before checking if there are messages to be sent to the server, so you need to set the period to TimeSpan.Zero if you want the messages to go as soon as possible.

var logger = new LoggerConfiguration()
            .WriteTo.Seq("http://localhost:5341", period: TimeSpan.Zero) // <---
            .CreateLogger();
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • The "period: TimeSpan.Zero" did not work for me. Still have to call Dispose() before I see logs. Anything I am missing? – SamDevx Oct 27 '17 at 00:41
  • 1
    Are you running a console app that is ending just after writing to the log? If that's the case, before your application ends, you should call `Log.CloseAndFlush();` see Lifecycle of Loggers - https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers – C. Augusto Proiete Jan 10 '18 at 22:12
1

I came across a similar issue while debugging. I realized that when you place a break point and debug, the calls to Seq won't happen unless you allow for a few seconds worth of processing time. The reason for that is because it holds on to the logs for a couple seconds before bulk sending whatever logs have queued up. How much and how often are configurable. By default, I believe it's 2 seconds.

The other thing you might try is to use the global context approach which works well for console applications.

Log.Logger = new LoggerConfiguration()
        .WriteTo.Seq("http://localhost:5341", period: TimeSpan.Zero) // <---
        .CreateLogger();

// Now you can make logging calls from anywhere in the application (main thread). 
Log.Debug("Test log");

There are also cases where certain exceptions and application exits will prevent the logs from being uploaded. Make sure you're giving your logs time to upload before your applications (or threads) exit for whatever reason.

GrayDwarf
  • 2,469
  • 2
  • 20
  • 22