Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispose of Serilog 2.0 correctly in ASP.NET Core application?

According to documentation, when an application is shutting down, it is recommended to dispose of Serilog using either

 Log.CloseAndFlush();

or in case you have an instance variable to .CreateLogger(), call its

.Dispose();

When building ASP.NET Core app in Visual Studio, it generates Program and Startup classes, neither of which implements IDisposable. Are we supposed to add this interface to Program so that we have a way to dispose Serilog directly?

like image 275
SamDevx Avatar asked Oct 31 '22 00:10

SamDevx


1 Answers

As far as I know, ASP.NET calls into dispose of the logger providers and so its the responsibility of the individual providers to do any clean up.

But looks like Serilog's logger provider does not do anything in its dispose method: https://github.com/serilog/serilog-extensions-logging/blob/master/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs

I would suggest to post a question on the Serilog's repo.

like image 200
Kiran Avatar answered Jan 04 '23 14:01

Kiran