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?
The answers you are receiving are partly correct. The
Threshold
is only used to set a lower limit on the level appended. A threshold of ERROR will actually receive ERROR and FATAL (which is "above" ERROR).You do want to implement a
LevelMatchFilter
with a level value of "ERROR" (and "DEBUG" for the other appender). However, you must also add aDenyAllFilter
to the end of your filter chain.For example:
I've had to implement the DenyAllFilter since log4net 1.2.10 (you can see the question I had on this topic here).
Note also that the
level
tag in the logger doesn't work the same way asthreshold
or aLevelMatchFilter
.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...
I've created a sample console application using your log4net config and I'm getting the exact behaviour you appear to be wanting....
When I run this application, the only thing in the logfile is:
And to the screen I see:
Here is the entirety of my app.config file:
You need to use additivity property. See here for an example. You need to define two loggers.
To get very specific filtering for an appender, you need to configure a
LevelMatchFilter
or aLevelRangeFilter
for the logfile appender to filter the events which are actually output.For example:
or
put one of these inside your
<appender>
tag, and this should work for you:Note: Updated to remove mistake pointed out by kgiannakakis.