【问题标题】:passing values to nlog custom target将值传递给 nlog 自定义目标
【发布时间】:2013-06-08 09:32:18
【问题描述】:

我创建了一个自定义的 nLog 目标,除了一件事之外它正在工作。 我想将登录的人名传递给目标,我创建了一个名为ApplicationUser 的属性,它将获取值。

目标定义如下所示:

<target name="MemoryTrace" 
        xsi:type="CustomTraceListener" 
        ApplicationUser="${identity:authType=False:isAuthenticated=False}"  />

但是当 customTarget 收到 ApplicationUser 的值时,它不会解析为登录的人名,它只是保持为${identity:authType=False:isAuthenticated=False}

这都是根据 nLog documentation

我已经用其他目标对此进行了测试,它得到了解决。我必须做什么才能将其解析为用户名?

【问题讨论】:

    标签: c# asp.net asp.net-mvc logging nlog


    【解决方案1】:

    您需要使用Layout 类型定义您的ApplicationUser,然后您可以使用ApplicationUser.Render(logevent) 获取当前用户“渲染布局”:

    [Target("CustomTraceListener")]
    public sealed class MyFirstTarget : TargetWithLayout
    {
    
        public Layout ApplicationUser { get; set; }
    
        protected override void Write(LogEventInfo logEvent)
        {
            string logMessage = this.Layout.Render(logEvent);
            string applicationUser = ApplicationUser.Render(logEvent);
            // ... Write where you want
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多