NLog Web - 没有域的 AspNet-User-Identity
NLog Web - AspNet-User-Identity without domain
我目前正在使用 NLog.Web 包在我的应用程序中编写我的 .Net 日志。
阅读 NLog.Web 后,我注意到与 ${windows-identity}
布局渲染器不同,${aspnet-user-identity}
布局渲染器没有域参数。
例如,如果我想记录当前运行 windows身份,它会注销:domain\user
,但当指定domain=false
时,它只会记录用户.
如何使用 ${aspnet-user-identity}
实现这种能力?因为我配置的时候${aspnet-user-identity:domain=false}
没用
NLog中使用的WindowsIdentity.Name
,总是给出全名,包括域。
The logon name is in the form DOMAIN\USERNAME.
我认为您需要一个自定义布局渲染器,并将其拆分 by-hand 在 /
。
类似这样的事情:(也许还会为 outOfIndex 添加 soms 检查)
using NLog.LayoutRenderers;
....
//register ${my-aspnet-user-identity}
LayoutRenderer.Register("my-aspnet-user-identity",
(logEvent) => HttpContext.Current?.User?.Identity?.Name?.Split('/')[1]);
尽快注册。
我找到了解决这个问题的不同方法@Julian
在 NLog.config 文件中,我创建了一个变量:
<variable name="aspnetIdentity" value="${replace:searchFor=^\w+\\:replaceWith=:regex=true:inner=${aspnet-user-identity}}" />
如变量中所定义,正则表达式至少搜索一个词(在开头),最后搜索反斜杠。
其他反斜杠是为了转义特殊字符和双反斜杠而编写的。最后,找到的内容(它是域名)将被替换为一个空字符串,因此我只得到了用户名而不是 Domain\Username
感谢@Julian 的帮助
我目前正在使用 NLog.Web 包在我的应用程序中编写我的 .Net 日志。
阅读 NLog.Web 后,我注意到与 ${windows-identity}
布局渲染器不同,${aspnet-user-identity}
布局渲染器没有域参数。
例如,如果我想记录当前运行 windows身份,它会注销:domain\user
,但当指定domain=false
时,它只会记录用户.
如何使用 ${aspnet-user-identity}
实现这种能力?因为我配置的时候${aspnet-user-identity:domain=false}
没用
NLog中使用的WindowsIdentity.Name
,总是给出全名,包括域。
The logon name is in the form DOMAIN\USERNAME.
我认为您需要一个自定义布局渲染器,并将其拆分 by-hand 在 /
。
类似这样的事情:(也许还会为 outOfIndex 添加 soms 检查)
using NLog.LayoutRenderers;
....
//register ${my-aspnet-user-identity}
LayoutRenderer.Register("my-aspnet-user-identity",
(logEvent) => HttpContext.Current?.User?.Identity?.Name?.Split('/')[1]);
尽快注册。
我找到了解决这个问题的不同方法@Julian
在 NLog.config 文件中,我创建了一个变量:
<variable name="aspnetIdentity" value="${replace:searchFor=^\w+\\:replaceWith=:regex=true:inner=${aspnet-user-identity}}" />
如变量中所定义,正则表达式至少搜索一个词(在开头),最后搜索反斜杠。 其他反斜杠是为了转义特殊字符和双反斜杠而编写的。最后,找到的内容(它是域名)将被替换为一个空字符串,因此我只得到了用户名而不是 Domain\Username
感谢@Julian 的帮助