Net Core NLog.Web“ aspnet用户身份”为空?

kiz*_*lsu 5 nlog asp.net-core-mvc

我使用“ NLog.Extensions.Logging”进行日志记录,并且需要记录用户身份,发现使用“ NLog.Web.AspNetCore”是可能的。“ nlog.config”文件被配置为记录“ aspnet-user-identity”。但是,当我查看日志时,用户身份部分始终为空字符串,其他列看起来也不错。我想念什么吗?

我的配置文件的一部分在这里:

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

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

<logger name="*" minlevel="Trace" appendTo="database"/>
Run Code Online (Sandbox Code Playgroud)

和一条插入命令将带有“ @identity”参数的日志插入数据库,但是就像我说的那样,它始终是空的。

Ron*_*ael 5

接受的答案不适用于我的案例(在 ASP.NET Core 3.1 上使用 JwtSecurityToken),后来意识到我只是忘记在 JwtSecurityToken 声明上添加 ClaimTypes.Name。

现在它可以工作了,不需要注册 IHttpContextAccessor。


Jul*_*ian 3

我想我已经找到问题了

有一个重大更改,默认情况下不再注册 IHttpContextAccessor 服务。(见公告

所以在你的startup.cs中添加:

public void ConfigureServices(IServiceCollection Services)
{
    //call this in case you need aspnet-user-authtype/aspnet-user-identity
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
Run Code Online (Sandbox Code Playgroud)