Allowing a logger to override a specific appender's parameter?
Using log4net, can I configure a logger so that it can override a single parameter of one of the appenders? Some examples:
<log4net>
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="log4net.log开发者_如何学C">
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="A1" />
</root>
<logger name="MyLogger">
<level value="WARN" />
<appender-ref ref="A1">
<file value="MyLogger.Log"/>
</appender-ref>
</logger>
</log4net>
For example, I'd like to configure one of my sub-loggers to send output to a different file from the rest of my application. Am I going the wrong way? Should I configure it with a new appender? What about sending different levels to different appenders? e.g.
<log4net>
<appender name="default" type="log4net.Appender.FileAppender">
<file value="My.log">
</appender>
<appender name="errors" type="log4net.Appender.FileAppender">
<file value="Errors.log">
</appender>
<root>
<level value="DEBUG">
<appender-ref ref="default" />
</level>
<level value="ERROR">
<appender-ref ref="errors" />
</level>
</root>
</log4net>
I have added this as a new answer as it is too long to fit in a comment.
Try something like
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]%date " />
<footer value="[Footer]%date " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="MyLogger" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="MyLogger.Log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]%date " />
<footer value="[Footer]%date " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="A1" />
</root>
<logger name="SpecialMethod">
<level value="DEBUG" />
<appender-ref ref="MyLogger" />
</logger>
</log4net
I use something similar to your first xml file. (With Log4net for C#)
Don't think the second xml will work, because, as I understand it, it would interpret <level value="DEBUG">
as setting the logging level to DEBUG , whereas you are wanting it to interpret as
if (Level == DEBUG) ...
精彩评论