Log4net - modify logging levels for individual logger and appender combinations -
i have 2 appenders, 1 (infoappender) levelmin info filter , 1 (debugappender) without filter. works great. can set minimum logging levels individual loggers using element. want have loggers logging info (and above) infoappender , debug (and above) debugappender, chatty logger (nhibernate) logging warn (and above) infoappender , info (and above) debugappender.
i looked @ solution provided in log4net logging of 2 different levels 2 different appenders same logger, doesn't work me. have following configuration:
<log4net> <appender name="infoappender" type="log4net.appender.rollingfileappender"> <file value="c:\logs\" /> <appendtofile value="true" /> <rollingstyle value="date" /> <datepattern value="'info.'yyyy-mm-dd'.log.txt'"/> <staticlogfilename value="false" /> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> <filter type="log4net.filter.levelrangefilter"> <levelmin value="info" /> </filter> </appender> <appender name="debugappender" type="log4net.appender.rollingfileappender"> <file value="c:\logs\" /> <appendtofile value="true" /> <rollingstyle value="date" /> <datepattern value="'debug.'yyyy-mm-dd'.log.txt'"/> <staticlogfilename value="false" /> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <logger name="nhibernate" additivity="false"> <appender-ref ref="infoappender"> <threshold value="warn" /> </appender-ref> <appender-ref ref="debugappender"> <threshold value="info" /> </appender-ref> </logger> <root> <level value="debug" /> <appender-ref ref="infoappender" /> <appender-ref ref="debugappender" /> </root> </log4net>
i have following test code:
xmlconfigurator.configure(); var logger1 = logmanager.getlogger("program"); var logger2 = logmanager.getlogger("nhibernate"); logger1.debug("debug message"); logger2.debug("debug message"); logger1.info("info message"); logger2.info("info message"); logger1.warn("warn message"); logger2.warn("warn message");
but both debug messages logged debugappender , both info messages logged infoappender.
the configuration mechanism of log4net quite lenient doesn't tell when parameters not taken account. example in configuration following references appender infoappender
threshold property doesn't anything
<appender-ref ref="infoappender"> <threshold value="warn" /> </appender-ref>
no parsing of xml element below appender-ref
node occur, put won't taken account:
protected void parsechildrenofloggerelement(xmlelement catelement, logger log, bool isroot) // code ommited if (xmlelement.localname == "appender-ref") { iappender appender = this.findappenderbyreference(xmlelement); string attribute = xmlelement.getattribute("ref"); if (appender != null) { loglog.debug(string.concat(new string[] { "xmlhierarchyconfigurator: adding appender named [", attribute, "] logger [", log.name, "]." })); log.addappender(appender); } else { loglog.error("xmlhierarchyconfigurator: appender named [" + attribute + "] not found."); } }
what need add filter between logger , final appender. recommend using forwardingappender
lets pass log events between appenders while adding behavior: change nhibernate section of config file this:
<appender name="nhibernatedebugfilterappender" type="log4net.appender.forwardingappender"> <appender-ref ref="debugappender"/> <filter type="log4net.filter.levelrangefilter"> <levelmin value="info" /> </filter> </appender> <appender name="nhibernateinfofilterappender" type="log4net.appender.forwardingappender"> <appender-ref ref="infoappender"/> <filter type="log4net.filter.levelrangefilter"> <levelmin value="warn" /> </filter> </appender> <logger name="nhibernate" additivity="false"> <appender-ref ref="nhibernateinfofilterappender" /> <appender-ref ref="nhibernatedebugfilterappender"/> </logger>
Comments
Post a Comment