Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core NLog.Web "aspnet-user-identity" is empty?

I was using "NLog.Extensions.Logging" for logging and need to log user identity and found that it is possible with "NLog.Web.AspNetCore". "nlog.config" file is configured to log the "aspnet-user-identity". However, when I look at logs, user identity part is always empty string, the other columns are look pretty good. Am I missing something?

A part of my configuration file is here:

<extensions>
  <assembly="NLog.Web.AspNetCore" />
</extensions>

<parameter name="@identity" layout="${aspnet-user-identity}"/>

<logger name="*" minlevel="Trace" appendTo="database"/>

And an insert command insert a log to db with "@identity" parameter, but it is always empty like I said.

like image 268
kizilsu Avatar asked Nov 21 '16 08:11

kizilsu


2 Answers

Accepted answer didn't worked for my case (using JwtSecurityToken on ASP.NET Core 3.1), Later realized that I just forgot to add the ClaimTypes.Name on my JwtSecurityToken Claims.

Now its working and no need to register IHttpContextAccessor.

like image 66
Ron Michael Avatar answered Nov 24 '22 00:11

Ron Michael


I think I have found the issue,

There was an breaking change, The IHttpContextAccessor service is not registered by default anymore. (See announcement)

So add in your startup.cs:

public void ConfigureServices(IServiceCollection Services)
{
    //call this in case you need aspnet-user-authtype/aspnet-user-identity
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
like image 28
Julian Avatar answered Nov 23 '22 22:11

Julian