How do I programmatically check that IsEnabledFor
is true
for a certain appender filter.
This is my config:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="appender" />
</root>
<appender name="appender" type="log4net.Appender.FileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<log4net>
Ok so if I set <root>
Level to say ERROR
and I do a IsEnabledFor
(Debug) it returns true
but if I set <root>
level to ALL
and add filters to the appender its not taking into account the filters.
How do I get it to include appender filters or is there another way to query this ?
I could be wrong, but I do not see how this can be done easily. What you could do is something like this:
var hierarchy = LogManager.GetRepository() as Hierarchy;
if(hierarchy != null)
{
var appenders = hierarchy.GetAppenders();
foreach( IAppender appender in appenders)
{
var appenderSkeleton = a as AppenderSkeleton
if (appenderSkeleton != null)
{
IFilter filterHead = appenderSkeleton.FilterHead;
// now analyse the filter chain
}
}
}
I did not test this code, but it should work more or less. What you get this way is the head of any configured filter chain for all appenders that derive from AppenderSkeleton
. The head of the filter chain implements IFilter
(as do all filters) and it contains one method and a property:
FilterDecision Decide(LoggingEvent loggingEvent);
IFilter Next { get; set; }
Now you could create an instance of a LoggingEvent
with the log level you are interested in, and go through the filter chain by using the Next
property and test each filter by calling FilterDecision
.
This tells you if the appender would accept or deny the logging event, but you need to be aware that an appender might also filter based on other criteria (e.g. message content) and thus it would not exactly be an "IsEnabledFor" implementation.