2015-09-17 75 views
2

我想寫蓋章事件的JSON日誌(目前只是一個ValueStamp,每個日誌條目需要一個唯一的GUID)。我跟着引導here和我log4net.config看起來像這樣log4net.ext.json - 自定義郵票

<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」郵票?

回答

2

我終於明白了 - 我需要把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」郵票。