log4net.ext.json - 自定义邮票
log4net.ext.json - Custom Stamps
我正在尝试编写带有标记事件的 JSON 日志(目前只是一个 ValueStamp,每个日志条目需要一个唯一的 guid)。我遵循了指南 here,我的 log4net.config 看起来像这样
<log4net>
<root>
<loggerFactory type='log4net.Util.Stamps.StampingLoggerFactory, log4net.Ext.Json'>
<stamp type='log4net.Util.Stamps.ValueStamp, log4net.Ext.Json'>
<name>stamp</name>
<value>MyValueHere</value>
</stamp>
</loggerFactory>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="JsonFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
...
</appender>
<appender name="JsonFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.json" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
<decorator type="log4net.Layout.Decorators.StandardTypesFlatDecorator, log4net.Ext.Json" />
<default />
<!--explicit default members-->
<member value="Stamp:stamp" />
</layout>
</appender>
</log4net>
不幸的是,我在日志消息中看到的只有
{"date":"2015-09-16T17:19:13.7833747-07:00","level":"INFO","appname":"MyApp.exe","logger":"MyApp.Program","thread":"6","ndc":"(null)","message":"Processed request successfully","Stamp":"stamp"}
所以我的问题是:
如何获得 Json 布局以显示 "MyValueHere" 邮票?
我终于想通了 - 我需要将 loggerFactory 放在根元素之前(和之外)。一个有效的 log4net.config 看起来像这样:
<log4net>
<loggerFactory type="log4net.Util.Stamps.StampingLoggerFactory, log4net.Ext.Json">
<stamp type="log4net.Util.Stamps.ValueStamp, log4net.Ext.Json">
<name>stamp</name>
<value>MyValueHere</value>
</stamp>
</loggerFactory>
<root>
<level value="INFO" />
<appender-ref ref="JsonFileAppender" />
</root>
<appender name="JsonFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.json" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
<decorator type="log4net.Layout.Decorators.StandardTypesFlatDecorator, log4net.Ext.Json" />
<member value="Stamp:stamp"/>
<default />
</layout>
</appender>
</log4net>
使用此配置,每个日志条目都会带有 "MyValueHere" 标记。
我正在尝试编写带有标记事件的 JSON 日志(目前只是一个 ValueStamp,每个日志条目需要一个唯一的 guid)。我遵循了指南 here,我的 log4net.config 看起来像这样
<log4net>
<root>
<loggerFactory type='log4net.Util.Stamps.StampingLoggerFactory, log4net.Ext.Json'>
<stamp type='log4net.Util.Stamps.ValueStamp, log4net.Ext.Json'>
<name>stamp</name>
<value>MyValueHere</value>
</stamp>
</loggerFactory>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="JsonFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
...
</appender>
<appender name="JsonFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.json" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
<decorator type="log4net.Layout.Decorators.StandardTypesFlatDecorator, log4net.Ext.Json" />
<default />
<!--explicit default members-->
<member value="Stamp:stamp" />
</layout>
</appender>
</log4net>
不幸的是,我在日志消息中看到的只有
{"date":"2015-09-16T17:19:13.7833747-07:00","level":"INFO","appname":"MyApp.exe","logger":"MyApp.Program","thread":"6","ndc":"(null)","message":"Processed request successfully","Stamp":"stamp"}
所以我的问题是: 如何获得 Json 布局以显示 "MyValueHere" 邮票?
我终于想通了 - 我需要将 loggerFactory 放在根元素之前(和之外)。一个有效的 log4net.config 看起来像这样:
<log4net>
<loggerFactory type="log4net.Util.Stamps.StampingLoggerFactory, log4net.Ext.Json">
<stamp type="log4net.Util.Stamps.ValueStamp, log4net.Ext.Json">
<name>stamp</name>
<value>MyValueHere</value>
</stamp>
</loggerFactory>
<root>
<level value="INFO" />
<appender-ref ref="JsonFileAppender" />
</root>
<appender name="JsonFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.json" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
<decorator type="log4net.Layout.Decorators.StandardTypesFlatDecorator, log4net.Ext.Json" />
<member value="Stamp:stamp"/>
<default />
</layout>
</appender>
</log4net>
使用此配置,每个日志条目都会带有 "MyValueHere" 标记。