log4net进程ID信息 [英] log4net process id information

查看:337
本文介绍了log4net进程ID信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个涉及多台机器上的多个进程的日志记录解决方案.我计划使用UDPAppender将所有日志消息发送到一台将对其进行管理的计算机.我对模式字符串与模式布局有一些疑问.

I am trying to create a logging solution that involves multiple processes on multiple machines. I planned on using the UDPAppender to send all log messages to a single machine that would manage them. I have a few questions about patternstrings vs patternlayouts.

因为我需要知道日志消息来自哪台机器和哪个进程,所以我也希望将其包括在日志中.我发现%property {log4net:HostName}作为主机名,并且效果很好.但是,我在PatternLayouts中看不到进程ID的任何内容.我当然会在PatternString中看到类似的内容.通过常见问题解答:

Because I need to know both which machine and which process that log message came from, I want to include that in the log as well. I found %property{log4net:HostName} for hostname, and that works great. However, I don't see anything for process id in PatternLayouts. I do, of course, see something like that in the PatternString. From the FAQ:

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />

    <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>

但是我不确定是否或如何将两者混合并匹配(或者即使这是规范的方式).

But I am not sure if or how to mix and match the two (or even if this is the canonical way to do so).

所以,我的问题是:

  1. PatternString和PatternLayout有什么区别?为什么两者都有?

  1. What is the difference between PatternString and PatternLayout? Why have both?

我在PatternString中看到了%processid,如何在PatternLayout中得到相同的值?这是我的测试布局:

I see the %processid in PatternString, how do I get the same in PatternLayout? Here is my test layout:

<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] [%property{log4net:HostName}] %-5level %logger  - %message%newline" />
</layout>

  • 最后,将XML布局用于UDP附加程序是有意义的.看起来XmlLayoutSchemaLog4j已经将HostNameProperty添加到XML消息中.如果我不想将此新的进程ID(可能还有进程名称)添加到XML消息中,执行此操作的最佳方法是什么?我是否应该只复制src \ Layouts \ XmlLayoutSchemaLog4j.cs,对其进行修改,然后让log4net知道我创建了这个新版式(例如SampleLayoutsApp)?

  • Finally, it makes sense to use the XML layout for the UDP appender. It looks like the XmlLayoutSchemaLog4j already adds the HostNameProperty to the XML message. If I wan't to add this new Process ID (and maybe Process Name) to the XML message, what is the best way to do this? Should I just copy src\Layouts\XmlLayoutSchemaLog4j.cs, modify it, and let log4net know that I created this new Layout (like the SampleLayoutsApp)?

    感谢您的帮助

    推荐答案

    您可以将所需的任何属性添加到GlobalContext.我使用此上下文存储进程ID,如下所示:

    You can add any properties you want to the GlobalContext. I use this context to store the process id, like this:

    log4net.GlobalContext.Properties["pid"] = Process.GetCurrentProcess().Id;
    

    然后,您使用常规模式从附加程序中引用此属性,如下所示:

    Then you reference this property from your appender using a regular pattern, like this:

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %property{pid} %level %logger - %message%newline" />
    </layout>
    

    您可以添加任意数量的属性,但是由于它是全局属性,因此最适合在应用程序执行期间不会更改的属性.

    You can add as many properties as you want, but because of it's global nature, it works best for properties that don't change during your application's execution.

    这篇关于log4net进程ID信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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