gpt4 book ai didi

c# - 将值传递给 nlog 自定义目标

转载 作者:行者123 更新时间:2023-11-30 20:57:07 27 4
gpt4 key购买 nike

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

目标定义如下所示:

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

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

这一切都是根据 nLog documentation

我已经用其他目标测试了这个并且它得到了解决。我需要做什么才能将其解析为用户名?

最佳答案

您需要使用 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
}
}

关于c# - 将值传递给 nlog 自定义目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com