【问题标题】:Net Core NLog.Web "aspnet-user-identity" is empty?Net Core NLog.Web“aspnet-user-identity”为空?
【发布时间】:2016-11-21 08:02:11
【问题描述】:

我使用“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"/>

插入命令使用“@identity”参数将日志插入数据库,但它总是像我说的那样为空。

【问题讨论】:

  • 您是否在 startup.cs 中调用了app.AddNLogWeb();?见github.com/NLog/NLog.web
  • 是的,我已经调用了@Julian 方法,忘记将其添加到我的问题中。
  • 它使用context.User.Identity.Name,其中contextGetService&lt;IHttpContextAccessor&gt;()。你能检查一下它是否仍然有效吗?

标签: asp.net-core-mvc nlog


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我在应用程序的一部分上使用常规登录,这些帐户显示为 layout="${aspnet-user-identity}",没有注册 IHttpContextAccessor。但它不适用于与 JwtSecurityTokens 一起使用的 API。注册声明类型 userClaims.Add(new Claim(ClaimTypes.Name, user.UserName));解决了这个问题。
【解决方案2】:

我在搜索“asp.net core nlog 用户名”时发现了这个问题。最终我找到了解决方案,并将其记录在这里以供后代使用。

问题

如何在我的 NLog 日志消息中包含当前登录用户的用户名?

解决方案

我在我的 ASP.NET Core Web 应用程序中使用 NLog。我尝试使用 ${aspnet-user-identity} 布局渲染器,但确定该值为空,因为我们使用的是自定义身份验证。

在进行了一些挖掘之后,我发现我可以从 NLog 访问存储为 ASP.NET 会话变量的值。因此,在对用户进行身份验证后,我将他们的用户名填充到会话变量中,瞧!

这是我在验证用户身份后调用的代码:

// Store username in session so we can access it from NLog logs.
httpContext.Session.SetString("NlogUser", contact.Username);

这是 nlog.config 中布局渲染器行的样子

<parameter name="@identity" layout="${aspnet-session:variable=NlogUser}"/>

这是AspNetSession layout renderer 对应的 NLog 文档。

【讨论】:

    【解决方案3】:

    我想我找到了问题,

    有一个重大更改,默认情况下不再注册 IHttpContextAccessor 服务。 (See announcement)

    所以添加你的 startup.cs:

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

    【讨论】:

    • 谢谢@朱利安!这就是问题所在:)
    • 我需要使用services.AddSingleton&lt;Microsoft.AspNetCore.Http.IHttpContextAccessor, HttpContextAccessor&gt;();,因为IHttpContextAccessor 在 NLog.Web 和 Microsoft.AspNetCore.Http 之间存在歧义。但它确实解决了它:-)
    【解决方案4】:

    如果您最近升级了 ASP.NET Core,您可能需要在 program.cs 中以不同方式配置 NLog:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                        .CaptureStartupErrors(true)
                        .UseIISIntegration()
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .UseStartup<Startup>()
                        .ConfigureLogging((hostingContext, logging) =>
                        {
                            //logging.AddNLog(); //<--- Can remove (NLog.Extensions.Logging)
                        })
                        ;
                })
                .UseNLog(); // <--- Call UseNLog off of IHostBuilder (NLog.Web)
    }
    

    https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2021-10-05
      • 2018-06-11
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2020-04-25
      相关资源
      最近更新 更多