Log4net appender threshold not working

Dav Evans picture Dav Evans · Jul 23, 2009 · Viewed 20k times · Source

I've set up a logfileAppender and a consoleAppender in my log4net config for my application. I would like the logfile appender to only write ERROR messages and above and the console appender to write DEBUG and above.

My config is:

<log4net debug="false">

<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
  <param name="File" value="log.txt" />
  <param name="AppendToFile" value="true" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%d %M - %m%n" />
  </layout>
  <threshold value="ERROR"/>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"   >
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d %m%n" />
  </layout>
</appender>


<root>
  <priority value="DEBUG" />
  <appender-ref ref="ConsoleAppender" />
  <appender-ref ref="LogFileAppender" />
</root>

</log4net>

I'm finding that both ERROR and DEBUG is being output to my logfile appender. How to restrict it to only ERROR?

Answer

G&#246;ran Roseen picture Göran Roseen · Dec 29, 2011

Note also that the level tag in the logger doesn't work the same way as threshold or a LevelMatchFilter.

Level indicates what log statements that actually will be generated. This is what you can test on in you code.

Threshold on the other hand, filters away all log messages that falls below your threshold.

This means that having a threshold that is higher than the highest logger level makes no sense. I have seen many times how one sets a level of INFO (because that is what most appenders will use), and then create an appender that has a threshold of DEBUG. And then you are surprised when no DEBUG messages actually appears on the appender...