nblumhardt / autofac-serilog-integration

Contextual logger injection for Autofac using Serilog
Apache License 2.0
70 stars 25 forks source link

Customization of the ILogger dependency configuration #19

Closed Driedas closed 7 years ago

Driedas commented 7 years ago

Hey, I really like the idea of this module for enriching log entries, however I have one issue with it - where to call Log.CloseAndFlush(). The module specifies the dependency as ExternallyOwned, however if in a project an ILogger instance is always retrieved via Autofac, I would like it to handle the Disposal as well (not relying on Application_End, etc).

How about adding a generic Lambda Action as another parameter to the SerilogContainerBuilderExtensions.RegisterLogger extension method, that would allow something like this:

Action<IRegistrationBuilder<ILogger, SimpleActivatorData, SingleRegistrationStyle>> registrationAction = registration =>
{
    registration.SingleInstance()
        .OnRelease(logger => Log.CloseAndFlush());
};

builder.RegisterLogger(registrationAction);

In order to be backwards compatible, the parameter would be nullable, and the default Action would specify the dependency as being .ExternallyOwned().

If you agree and like this approach, I can submit a PR with this change...

nblumhardt commented 7 years ago

Thanks for the note!

In Serilog.Extensions.Logging we provide a similar option via the dispose flag:

https://github.com/serilog/serilog-extensions-logging/blob/dev/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs#L22

Could we do something similar here?

Driedas commented 7 years ago

Sure, that's another option. If dispose: true is passed in, Autofac will assume ownership of the dependency and instead of .ExternallyOwned() the Log.Logger would get registered as .SingleInstance().OnRelease(logger => Log.CloseAndFlush()) right?

nblumhardt commented 7 years ago

@Driedas yes, I think that's the way it would work 👍

nblumhardt commented 7 years ago

Closed by #20 - will be published in a -dev- package by CI shortly. Thanks @Driedas !

Driedas commented 7 years ago

Thanks, 't was a lot of fun working on this, appreciate your input and time spent reviewing :-) And thanks for the awesomest logging library out there!