tags:

views:

1249

answers:

2

Ive 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>

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

A: 

To get very specific filtering for an appender, you need to configure a LevelMatchFilter or a LevelRangeFilter for the logfile appender to filter the events which are actually output. For example:

<filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR"/>
    <levelMax value="FATAL"/>
</filter>

or

<filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="ERROR"/>
</filter>

put one of these inside your <appender> tag, and this should work for you:

<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
    <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="ERROR"/>
    </filter>
    <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>

Note: Updated to remove mistake pointed out by kgiannakakis.

Vinay Sajip
It is actually the other way round. A Debug level means that Error messages should also be printed. In Error level debug messages aren't printed. Read this: http://ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html
kgiannakakis
+1 You're right, and I've removed the mistake. But how can you explain what the questioner is seeing?
Vinay Sajip
Yes it is the other way round.
Dav Evans
@Dav.evans, can you confirm that the above is the complete configuration? What loggers are you using? Is it definite that no configuration changes are being made programmatically in your code?
Vinay Sajip
A: 

You need to use additivity property. See here for an example. You need to define two loggers.

kgiannakakis
But logfile is an Appender, and additivity pertains to Loggers.
Vinay Sajip
I've corrected this. You actually create two loggers, each with one appender and use the additivity property.
kgiannakakis
surely not. What is the purpose of the threshold element then?
Dav Evans
Have you tried it? LogFileAppender inherites its parent (root) appenders by default, so it prints DEBUG messages by default. Try to set root's priority to ERROR to see what happens.
kgiannakakis
nup - didnt work
Dav Evans
@kgiannakakis, that isn't right - Loggers aren't Appenders. As the appenders are attached to the root logger, events logged at every logger will be processed by these appenders, unless the additivity of a particular *logger* is set to false in which case events at that logger and below would not be processed. In such a case the threshold of the appender should be working - which makes me think that perhaps not all the information has been posted?
Vinay Sajip