nblumhardt / autofac-serilog-integration

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

Custom ILogger registration? #7

Closed artganify closed 8 years ago

artganify commented 8 years ago

Currently, this integration wraps everything required to resolve context-aware instances of ILogger from Autofac in a single Autofac module. While extremely convenient (it really doesn't get any easier than builder.RegisterLogger()), I'm currently facing the problem that I need to inject instances of ILogger into imported MEF components (which are being resolved using Autofac with the Autofac.Integration.Mef package), however Autofac doesn't expose any dependencies for resolved MEF components unless they are declared as exports (using IRegistrationBuilder<...>.Export()). Which means I need to change how ILogger is being registered.

Is there a way do to that with this integration? All I can see is that the extension method optionally takes an existing instance of ILogger or allows to declare whether properties are being autowired. And it returns a IModuleRegistrar instead of a IRegistrationBuilder, which is normally expected when registering components on the ContainerBuilder.

artganify commented 8 years ago

Created a PR: #8

nblumhardt commented 8 years ago

Thank you for the PR! Not a scenario I'd given any thought to.

I'm a bit rusty on how the MEF integration works - would you be able to give a code sample showing how the updated API is used?

Regards, Nick

artganify commented 8 years ago

Using the updated api, I can register the logger like so:

builder.RegisterLogger(configureRegistration: x => x.Export());

That way, the logger is being declared as an export and I can inject it into a resolved MEF component using Autofac.

However I think it would be good idea to have this API generally, because if I'm going to register a component within my IoC, I want at least to be able to define how the component is being registered. Maybe I need a special lifetime? Maybe I want to provide additional metadata?

nblumhardt commented 8 years ago

Thanks for the follow-up! Makes sense.

It's a nice tidy PR, but given how small this project is I think it might be better in this case to grab the code and modify it in your own project. I'd like to keep the API of this one trivially simple, and I'm not sure exposing some of the implementation (i.e. generic arguments on IRegistrationBuilder<,,> will hold up over time. Would this work for you?

artganify commented 8 years ago

Yup, works for me. I cannot say anything about the consequences of exposing the generic IRegistrationBuilder<,,>, so this is probably for the best.

Thank you for your time. :)