tags:

views:

63

answers:

2

Hey,

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.

Regards, Harel

A: 

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.

Stefan Egli
Hey!Thanks for the quick reply.Can it be done programatically? i only found a way of changing the Logger's (root) log-level, not each appenders'.
Harel Moshe
sorry, I missed your comment. should be possible programatically as well. here you can see how to get to all appenders: http://stackoverflow.com/questions/3016108/change-log4net-conversion-pattern-or-layout-at-runtime/3031867#3031867
Stefan Egli
A: 

Use LevelRangeFilter

<filter type="log4net.Filter.LevelRangeFilter">
  <levelMax  value="FATAL" />
  <levelMin  value="ERROR" />
</filter>
Artur
Hey, as that's the natural usage form, i tried this option but it forces me to log also the messages with levels *between* FATAL and ERROR, which i don't want (even though ERROR and FATAL are adjacent, the principle still applies).Thanks anyway!
Harel Moshe