Basically I have the old known issue of an application crashing and having no debug logs, because the application terminated before it could write the logs.
A classic approach is to catch all Exceptions in main and to flush all log buffers in the exception block. Furthermore adding a sleep to reduce the restart speed and allow the buffers to finish the flush.
I use serilog, and I found this ticket: https://github.com/serilog/serilog/issues/1111
It suggests to do as I suggested (code pasted below), but I am uncertain it will close and flush all loggers and not just the main logger.
- Will I have to flush more loggers, to get all logs?
- This example assumes, that ConfigureLogging() sets up the logger, but in a real asp .netcore main you usually don't have the logger set up, how do I get to do that?
public static int Main(string[] args)
{
ConfigureLogging(); // set up Serilog
try
{
Log.Logger.Information("Custom.API Server is starting.");
var webHost = BuildWebHost(args);
webHost.Run();
Log.Logger.Information("Custom.API Server is starting.");
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly.");
return 1;
}
finally
{
Log.CloseAndFlush();
Thread.Sleep(2000); // give serilog / seq time to flush the messages
}
}