[英]passing values to nlog custom target
我已經創建了一個自定義的nLog目標,它除一件事外都在工作。 我想將登錄的人名傳遞給目標,我創建了一個名為ApplicationUser
的屬性,該屬性將獲取值。
目標定義如下所示:
<target name="MemoryTrace"
xsi:type="CustomTraceListener"
ApplicationUser="${identity:authType=False:isAuthenticated=False}" />
但是,當customTarget接收到ApplicationUser的值時,它不會解析為登錄到人的名字,而是停留為${identity:authType=False:isAuthenticated=False}
。
所有這些都按照nLog 文檔
我已經與其他目標進行了測試,並得到解決。 將其解析為用戶名該怎么辦?
您需要使用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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.