Log4Net filter out INFO from the log and only show DEBUG & ERROR
Asked Answered
E

2

10

Is there any way to filter out INFO from the log and only show DEBUG & ERROR, using the config in web.config ?

<root>
<level value="DEBUG" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="RollingFileSystemAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
Eyeful answered 4/4, 2014 at 12:36 Comment(1)
possible duplicate of Log4Net configuration - LevelMatchFilterDuhl
D
12

In log4X there are filters that can be applied to appenders in order to filter messages; here is a list of the filters

  • log4net.Filter.LevelMatchFilter Filters log events that match a specific logging level; alternatively this can be configured to filter events that DO NOT match a specific logging level.
  • log4net.Filter.LevelRangeFilter Similar to the LevelMatchFilter, except that instead of filtering a single log level, this filters on an inclusive range of contiguous levels.
  • log4net.Filter.LoggerMatchFilter Filters log events based on the name of the logger object from which they are emitted.
  • log4net.Filter.StringMatchFilter Filters log events based on a string or regular expression match against the log message.
  • log4net.Filter.PropertyFilter Filters log events based on a value or regular expression match against a specific context property.
  • log4net.Filter.DenyAllFilter Effectively drops all logging events for the appender.

In your case you would need to filter your three appenders in order to exclude the INFO level: a LevelMatchFilter refusing INFO level logs would work:

<filter type="log4net.Filter.LevelMatchFilter">
  <acceptOnMatch value="false" />
  <levelToMatch  value="INFO" />
</filter>
Duhl answered 7/4, 2014 at 8:59 Comment(1)
That is exactly what I was looking for!! Tested and it works. can't thank you enough.Eyeful
V
6

Note that the <filter> tags (as shown in the examples above) must appear within an <appender> tag! For example (this is not a valid log4net configuration--I'm only showing how the <filter> tag is a child entry under <appender>:

<configuration>
  <configSections>
    <section ...="" />
  </configSections>
  <startup>
    <supportedRuntime ...="" />
  </startup>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyyy HH:mm:ss} {%thread} %level - %message%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <acceptOnMatch value="true" />
        <levelToMatch  value="INFO" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <root>
      <appender-ref ...="" />
    </root>
    <logger name="Log4NetTest.OtherClass">
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
    </logger>
  </log4net>
</configuration>
Viridescent answered 23/6, 2016 at 14:18 Comment(1)
Upvoted for pointing out the correct location of the tag as well as the DenyAllFilter - which is required to block all the other levels.Revert

© 2022 - 2024 — McMap. All rights reserved.