Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discarding several log levels within a range with log4net

Tags:

log4net

Say I set my log4net logger's minLevel and maxLevel to FATAL and DEBUG respectively, but under some scenario I want to mute the log-items written in the WARN level, and keep all the other levels in the range active.

Is it possible to somehow use 'discrete' levels of log-levels rather than specifying a range using minLevel and maxLevel?

I assume this should be simple, but I haven't found any log4net docs or examples dealing with this issue.

like image 794
Harel Moshe Avatar asked May 14 '10 05:05

Harel Moshe


1 Answers

You can use the LevelMatchFilter on your appender.

Example:

<appender name="FilteredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="DEBUG" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
        <levelToMatch value="ERROR" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    ...
</appender>

This example will only print DEBUG; INFO and ERROR messages. It is easy to customize this according to your needs.

Note: Do not forget the DenyAllFilter at the end.

like image 56
Stefan Egli Avatar answered Oct 02 '22 01:10

Stefan Egli