我目前正在使用NLog.Web包在我的应用程序中编写.Net日志。在阅读了NLog.Web之后,我注意到与${windows-identity}布局呈现器不同的是,${aspnet-user-identity}布局呈现器没有为它获取域参数。
例如,如果我想记录当前运行的windows标识,它将注销:domain\user,但是当指定domain=false时,它只记录用户。
如何在${aspnet-user-identity}中实现这种能力?因为当我配置${aspnet-user-identity:domain=false}时,它没有工作。
发布于 2018-04-23 07:49:02
我找到了解决这个问题的另一种方法
在NLog.config文件中,我创建了一个变量:
<variable name="aspnetIdentity" value="${replace:searchFor=^\\w+\\\\:replaceWith=:regex=true:inner=${aspnet-user-identity}}" />正如在变量中定义的那样,regex搜索至少一个单词(在开头),最后搜索反斜杠。其他反斜杠是用来转义特殊字符和双反斜杠的。最后,找到的内容(它是域名)将被替换为空字符串,因此我只得到用户名,而不是域\用户名
“谢谢你的帮助”朱利安
发布于 2018-03-28 20:31:46
WindowsIdentity.Name (在NLog中使用)总是会给出全名,包括域。
登录名位于表单域\用户名中。
我认为您需要一个定制的布局渲染器,并在/上手工拆分它.
如下所示:(也可以为outOfIndex添加soms检查)
using NLog.LayoutRenderers;
....
//register ${my-aspnet-user-identity}
LayoutRenderer.Register("my-aspnet-user-identity",
(logEvent) => HttpContext.Current?.User?.Identity?.Name?.Split('/')[1]);请尽快登记。
https://stackoverflow.com/questions/49475759
复制相似问题