Question

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>
Was it helpful?

Solution

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>

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top