autofac-serilog-integration icon indicating copy to clipboard operation
autofac-serilog-integration copied to clipboard

Doesn't add SourceContext in ASP.Net 5 controllers with ILogger injected

Open eric-swann-q2 opened this issue 9 years ago • 7 comments

Doesn't add SourceContext in ASP.Net 5 controllers with ILogger injected

Controller get registered with the Registration.Preparing event, but doesn't appear that this ever gets called for a controller when the controller is instantiated by the container...not sure why.

eric-swann-q2 avatar Feb 04 '16 18:02 eric-swann-q2

Thanks for the heads-up Eric. I'm not sure about how this code will go on the new versions of ASP.NET/Autofac - I haven't tried updating it just yet.

If you drop the two main source files - ContextualLoggingModule.cs and the SerilogContainerBuilderExtensions.cs files directly into your project you may be able to debug through this scenario.

I'll try to investigate as soon as I have an opportunity - still a lot of work to do on Serilog fundamentals for v2 however :-)

nblumhardt avatar Feb 04 '16 20:02 nblumhardt

Sure no worries...I'm also pressed for time at the moment or i would look further...I'll take a look if i get a chance.

eric-swann-q2 avatar Feb 04 '16 20:02 eric-swann-q2

I have this same issue too. I've had a go at what @nblumhardt suggested and found that only adding SourceContext here had any effect.

It's really strange. This line of code gets executed but doesn't have any effect. Looking at the value of registration.Activator.LimitType in these calls, it seems like my controllers and middleware aren't coming in here. I'm not sure what to make of that?

I'm happy to play around with this if someone can point me in a direction of hope. It's obviously very useful to know where your log messages are coming from, and even more useful not to need to ForContext("SourceContext", GetTypeInfo().Name") every time you get an ILogger injected :+1:

zaccharles avatar Aug 08 '16 20:08 zaccharles

Thanks for the follow-up @zaccharles. Are you using Autofac 4.x? The machinery required for this is only in 4.0 (pre-release), not 3.x.

nblumhardt avatar Aug 08 '16 22:08 nblumhardt

Yeah I'm using 4.0.0-rc3-309.

zaccharles avatar Aug 08 '16 22:08 zaccharles

Interesting, thanks. I'm not sure whether the "Autofac.AutowiringPropertyInjector.InstanceType" parameter is present in all conditions - needs some debugging into the Autofac side of things I'd guess.

nblumhardt avatar Aug 08 '16 23:08 nblumhardt

Looking at this again, we should probably only call ForContext here if Activator.LimitType is not typeof(object).

https://github.com/nblumhardt/autofac-serilog-integration/blob/dev/src/AutofacSerilogIntegration/ContextualLoggingModule.cs#L95

(Same code repeats for property injection a few lines lower.)

nblumhardt avatar Aug 01 '17 21:08 nblumhardt