New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpContextAccessor is null in AspNetLayoutRendererBase #327
Comments
If wanting to configure NLog in Startup.cs using AddNLog (ASP NET Core v1 style): But the recommended way is UseNLog in Program.cs: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs Please do not apply both approaches at the same time. |
Now that is a buried nugget of gold. That did indeed solve my issue. Thank you. |
@jholovacs Remember not to use both AddNLog (ver. 1) and UseNLog (ver. 2) at the same time. |
@snakefoot Thanks, but it's not clear how I would be able to use the connection string in my appsettings from Program.cs before DI is configured. Can you point me to where I'd find that info? |
NLog variables should not be assigned at runtime. Instead use GDC see also:
https://github.com/NLog/NLog/wiki/Gdc-layout-renderer
|
I don't see how that would work to load a connection string that's stored in an appsettings.json file. I need the |
You just have to assign the GDC at any point at runtime. Before or after loading the NLog config.
|
I've got NLog wired into DI like so:
I've made my own test renderer (off of
AspNetLayoutRendererBase
) and registered it just to see why I could not get the user info to work.The ClaimsPrincipal is properly filled out with the user claims. The ClaimsIdentity has the proper username. The
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
method in the layout renderer was never reached... so I dug a bit further back and found that theprotected override void Append(StringBuilder builder, LogEventInfo logEvent)
method was being hit. However, theHttpContextAccessor
property was null, so it never got to theDoAppend
method.I see this method is populated with a
ServiceLocator
, but I don't know if maybe that can't figure out how to access theServiceProvider
in .NET Core 2.1, or do I need to configure something that I'm missing?NLog version: 4.5.10
NLog.Web/NLog.Web.AspNetCore version: 4.7.0
NLog.Extensions.Logging version: 1.3.0
Platform: .NET Core 2.1
Current NLog config (xml or C#, if relevant)
In case of a BUG:
IHttpContextAccessor
is registered as a Singleton in the application already, and it still not able to find it.The text was updated successfully, but these errors were encountered: