1
votes

I have a C# Framework 4.5.2 project, using log4net v2.0.8 (installed via NuGet), and I've read at least ten different but similar questions, and none of them have solved my issue. Sure, I can write successfully to my log appenders, but the filtering/threshold isn't working. I have a single logger (either using ROOT, or a named instance) and have set different thresholds/filters that should direct the actual logging to use a specific appender (i.e. file, db, smtp, etc). This way, a certain level of filter value might only get handled by a single appender. For testing, I'm simply trying to get the Warning and above thresholds to use the ADO appender, while the file appender logs the Info (and above) threshold. So, when I test an Info log method, it shouldn't end up in the db, but it always does. So, somehow I have the configuration wrong. Here are just a couple that I've looked through, and they have given me insight, but not a solution:

Here's some code:

Configuration:

  <!--<root>      
</root>-->
<logger name="Logger">
  <appender-ref ref="LogFileAppender"/>        
  <appender-ref ref="AdoNetAppender"/>           
</logger>

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="c:\temp\logging\derp.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="5" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %property{ProjectName} (%logger) - %message%newline" />
  </layout>
  <!--<threshold value="Info" />-->
</appender>

I've tried having a Threshold within the logger section's appender-ref, I've had it within the appender directly. I've used LevelMatchfilter, with both level and levelMin/levelMax (see below). LevelRangeFilter, nothing. Nothing correctly works.

<filter type="log4net.Filter.LevelMatchFilter">
    <levelMin value="Warning" />
    <levelMax value="Fatal" />
</filter>
<threshold value="Warning" />

I've tried incorporating Evaluator, and that doesn't work.

<evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="ERROR" />
</evaluator>

Most of the online examples are 5+ years old, often talking about v1.2. Is there something in the newer version of this, or am I just missing something else, or doing it in the wrong order? Any help is appreciated. Thank you!

1

1 Answers

1
votes

I see two things. First, It's WARN, not Warning.

<filter type="log4net.Filter.LevelMatchFilter">
    <levelMin value="WARN" />
    <levelMax value="Fatal" />
</filter>

Second, add the root like this.

<root>
  <level value="ALL" />
  <appender-ref ref="LogFileAppender" />
  <appender-ref ref="AdoNetAppender" />
</root>