简体   繁体   English

如何配置一个附加程序来记录所有log4net ThreadContext / GlobalContext / LogicalThreadContext属性?

[英]How to configure an appender to log all log4net ThreadContext/GlobalContext/LogicalThreadContext properties?

How to configure an appender to log all log4net ThreadContext/GlobalContext/LogicalThreadContext properties? 如何配置一个附加程序来记录所有log4net ThreadContext / GlobalContext / LogicalThreadContext属性?

I would like to use custom properties in my application and log everything into a textfile. 我想在应用程序中使用自定义属性,并将所有内容记录到文本文件中。 Every logmessage should be a valid JSON-Object. 每个日志消息都应该是有效的JSON对象。

I would like to include all properties from the different Context-Objects (Thread/LogicalThread/MDC/Global). 我想包括来自不同上下文对象(线程/逻辑线程/ MDC /全局)的所有属性。 But all examples I find need the knowledge of the property-key. 但是我发现的所有示例都需要了解属性键。

I found an answer which leads me to the thought, that this is not possible with the API at all: Log4Net: Enumerating GlobalContext properties? 我找到一个答案,使我想到,使用API​​根本不可能做到这一点: Log4Net:枚举GlobalContext属性吗?

If there is no official way to do this, would this code point to the correct direction? 如果没有官方的方法可以做到,那么这段代码会指向正确的方向吗?

log4net.ext.json seems to ignore the context-properties log4net.ext.json似乎忽略了上下文属性

simply just use below conversion pattern, the [%property] would cover all the properties in the context. 只需使用下面的转换模式,[%property]将覆盖上下文中的所有属性。

%date [%thread] %-5level %logger [%property] - %message%newline

For JSON object, then you need to write your own converter. 对于JSON对象,则需要编写自己的转换器。

With log4net.Ext.Json , one could use the property member: 使用log4net.Ext.Json ,可以使用property成员:

<appender...>
    <layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
        <decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
        <default /> <!--explicit default members-->
        <member value='property' />
    </layout> 
</appender>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM