C# 将值传递给nlog自定义目标
我已经创建了一个自定义非直瞄目标,它正在工作,除了一件事。 我想将登录的人名传递给目标,我创建了一个名为C# 将值传递给nlog自定义目标,c#,asp.net,asp.net-mvc,logging,nlog,C#,Asp.net,Asp.net Mvc,Logging,Nlog,我已经创建了一个自定义非直瞄目标,它正在工作,除了一件事。 我想将登录的人名传递给目标,我创建了一个名为ApplicationUser的属性,该属性将获取值 目标定义如下所示: <target name="MemoryTrace" xsi:type="CustomTraceListener" ApplicationUser="${identity:authType=False:isAuthenticated=False}" /> 但是,当c
ApplicationUser
的属性,该属性将获取值
目标定义如下所示:
<target name="MemoryTrace"
xsi:type="CustomTraceListener"
ApplicationUser="${identity:authType=False:isAuthenticated=False}" />
但是,当customTarget接收到ApplicationUser的值时,它不会解析为登录的人的名字,它只是作为${identity:authType=False:isAuthenticated=False}
这一切都符合nLog
我已经用其他目标测试了这个问题,它得到了解决。要将其解析为用户名,我需要做什么?您需要使用类型
布局定义ApplicationUser
,然后您可以使用ApplicationUser.Render(logevent)
“呈现布局”获取当前用户:
[目标(“CustomTraceListener”)]
公共密封类MyFirstTarget:TargetWithLayout
{
公共布局应用程序用户{get;set;}
受保护的重写无效写入(LogEventInfo logEvent)
{
字符串logMessage=this.Layout.Render(logEvent);
字符串applicationUser=applicationUser.Render(logEvent);
//…写你想写的地方
}
}
您需要使用类型布局定义您的应用程序用户
,然后您可以使用ApplicationUser.Render(logevent)
通过“呈现布局”获取当前用户:
[目标(“CustomTraceListener”)]
公共密封类MyFirstTarget:TargetWithLayout
{
公共布局应用程序用户{get;set;}
受保护的重写无效写入(LogEventInfo logEvent)
{
字符串logMessage=this.Layout.Render(logEvent);
字符串applicationUser=applicationUser.Render(logEvent);
//…写你想写的地方
}
}