serilog / serilog-extensions-logging-file

Add file logging to ASP.NET Core apps in one line of code.
Apache License 2.0
154 stars 42 forks source link

Option to use synchronous logging #59

Open JobaDiniz opened 2 years ago

JobaDiniz commented 2 years ago

On short-lived applications such as command line interfaces, using async is not optional and logs are not written.

Can we add an option to the AddFile to use synchronous log?

nblumhardt commented 2 years ago

Hi! I think you can get the behavior you want by disposing your ServiceProvider before the application exits - does this help? Flush-per-event is generally very slow (although it's supported by the underlying Serilog file sink).

Best regards, Nick

JobaDiniz commented 2 years ago

I'm using System.CommandLine package and their ServiceProvider is internal and does not implement IDisposable.

I think serilog-extensions-logging-file shouldn't assume we're using Microsoft.Extensions.DependencyInjection.ServiceProvider. Although such class is disposable, the IServiceProvider interface is not IDisposable.

Flush-per-event is generally very slow.

I think that's fine for short-live applications such as CLI. I believe making async is slower for CLI to be honest.

Which instance from Serilog do I need to dispose in order to flush the logs before the application exits?

nblumhardt commented 2 years ago

Hi! This might be valuable feedback for the System.CommandLine folks; though, my guess is that they'll already have some APIs available for switching IoC containers etc. It's pretty common for services to require disposal - plugging in a regular container, or using the generic host via UseHost() should sort this out.

If you were to use Serilog directly instead, then disposing the Serilog Logger, or a SerilogLoggerProvider wrapping it, would be the way to go.

It sounds like this is a bit of a pain at present, but I don't think it's something within the scope of what this simplified package should address; disposing loggers to flush them is extremely common (as is disposal of all kinds of services).

Hope this helps, Nick