My question may seem a little strange - I wish to log the conversionPattern used to format the messages logged with log4net when certain appenders are used.
For instance, if my log4net section looks like this:
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="MainAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="${NCLOGS}\NC.Entities.Test${CI_TAG}.%processid.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<MaximumFileSize value="10MB" />
<StaticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="MainAppender" />
</root>
</log4net>
Then I would like to see %date [%thread] %-5level %logger - %message%newline in the log file.
Rationale I am using Log4View to view our logs and 开发者_如何学JAVAit demands the conversion pattern in order to be able to format the messages. I find it convenient if the conversion pattern is found in the log itself, this spares me the need to look for the respective config file and copy it from there.
Right now I am doing using some XPATH magic, but I am wondering if there is a better solution, probably using some log4net API.
Thanks.
One way to do it would be to add the pattern as a property when you start up the application. You could read the pattern from the config file or there might be a way to get the pattern for each of the appenders programatically, in which case you can add one property for each appender and then have each pattern for each appender log the property.
Something like this to add the property:
log4net.GlobalContext.Properties["PatternForAppender"]= getLoggerPattern();
And something like this to include it in the log:
<conversionPattern value="%logger (%property{PatternForAppender}) [%level] - %message%newline" />
精彩评论