将当前用户添加到NLog输出 [英] Add current user to NLog output

查看:205
本文介绍了将当前用户添加到NLog输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NLog输出的每一行中添加一个字符串.逻辑保持不变,尝试获取当前用户,如果成功,则将当前用户添加到输出中.

I want to add a string to each row on my NLog output. The logic stays the same, trying to get the current user and if succeed, add the current user to the output.

我每次都知道如何实现它,但是我想将此模板设置在一个位置,而不是在每次写作时都重复它.

I know how to implement it each time, but I want to set this template at one place and not repeat it on each writing.

推荐答案

WindowsIdentityLayoutRenderer应该会给您想要的东西.您可以选择记录域,用户名或两者.

The WindowsIdentityLayoutRenderer should probably give you what you want. You can choose to log either the Domain, the UserName, or both.

您可以将其配置为NLog.config文件(未调试):

You would configure it something like this (untested) to your NLog.config file:

<targets>
    <target name="file" xsi:type="File" 
        layout="${longdate} | ${level} | ${logger} | ${windows-identity} | ${message}"
        fileName="${basedir}/${shortdate}.log" />
</targets>

这可能在低特权环境中不起作用.

This might not work in a low privilege environment.

您现在如何获得用户名?如果您得到这样的信息:

How do you get the user name now? If you get it something like this:

HttpContext.Current.User.Identity.Name

然后,您可以使用NLog的"aspnet-user-identity" LayoutRenderer,如下所示:

Then you can use NLog's "aspnet-user-identity" LayoutRenderer, something like this:

<targets>
    <target name="file" xsi:type="File" 
        layout="${longdate} | ${level} | ${logger} | ${aspnet-user-identity} | ${message}"
        fileName="${basedir}/${shortdate}.log" />
</targets>

NLog的aspnet * LayoutRenderers位于NLog.Extended.sll中,因此除了NLog.dll外,您还需要该dll.

NLog's aspnet* LayoutRenderers are in NLog.Extended.sll, so you will need that dll in addition to NLog.dll.

这篇关于将当前用户添加到NLog输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆