Say I set my log4net logger's minLevel
and maxLevel
to F开发者_如何学JAVAATAL
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.
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.
It's cool how log4net filters can all be combined to achieve the desired output. All log entries by default have a "Neutral" filter disposition and log4net by default logs all entries that are neutral.
What the LevelRangeFilter
will do is if the level of the entry is in the range, it will set the filter disposition to "Accept" (or leave its disposition as it was if the acceptOnMatch
parameter is set to false
) and it will mark all entries not in the range with a disposition of "Deny".
The LevelMatchFilter
will set the disposition for the filter specified in the levelToMatch
parameter to "Accept" unless the acceptToMatch
is set to false
, then it will set matching entries to "Deny", unmatched entries will be left to what they were before.
So what you can do is use a combination of the two filters to get what you want:
<appender name="FilteredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMax value="FATAL" />
<levelMin value="ERROR" />
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="WARN" />
<acceptOnMatch value="false" />
</filter>
</appender>
This will allow you to easily toggle the warn level on and off. All entries outside of the range are already all marked as "Deny" and the LevelMatchFilter
here will mark WARN level entries to Deny as well so the DenyAllFilter
is not required.
Use LevelRangeFilter
<filter type="log4net.Filter.LevelRangeFilter">
<levelMax value="FATAL" />
<levelMin value="ERROR" />
</filter>
精彩评论