Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I flush logging before terminating the actor system?

I'm using Serilog and I notice when shutting down the actor system, not all outstanding log messages have been processed before custom loggers like Serilog are removed. Because of this, a bunch of messages are no longer sent to the Serilog sink and they end up in the default logger instead.

As a workaround, I use this in a ReceiveAsync handler:

await Task.Delay(TimeSpan.FromSeconds(5));
Context.System.Terminate();

I can probably use an AutoResetEvent or similar in a non-async handler or an FSM OnTermination handler.

The workaround above is just that though, a workaround. Is there a way to flush the logs? Note that I already flush Serilog before shutting down the actor system, that part works fine.

like image 713
user247702 Avatar asked Nov 08 '22 11:11

user247702


1 Answers

If you're using the global Log class, call Log.CloseAndFlush() before termination.

If you're using an IoC container or similar to manage a Logger instance separately, that's not assigned to the global Log.Logger property, make sure the container (or your app code) Dispose()s the logger instance before termination.

like image 163
Nicholas Blumhardt Avatar answered Nov 25 '22 15:11

Nicholas Blumhardt