Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 552
Samurai Jack Avatar asked Apr 04 '14 12:04

Samurai Jack


People also ask

How do I completely disable all logging at runtime?

How do I completely disable all logging at runtime? Setting the Threshold on the Hierarchy to Level OFF will disable all logging from that Hierarchy. This can be done in the log4net configuration file by setting the "threshold" attribute on the log4net configuration element to "OFF".

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

Is log4net dependent on log4j?

Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.


1 Answers

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>
like image 195
samy Avatar answered Oct 02 '22 20:10

samy