Reputation: 2378
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.
Upvotes: 5
Views: 4768
Reputation: 2747
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.
Upvotes: 2
Reputation: 27868
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();
Upvotes: 1