[ACCEPTED]-log4net: Configure to ignore messages from a specific class-log4net
Sure, use a filter.
Here's the snippet posted on 2 the blog, for future reference - all credit 1 to the author of that blog post:
<filter type="log4net.Filter.LoggerMatchFilter">
<!-- allows this sub-namespace to be logged... -->
<loggerToMatch value="Noisy.Namespace.But.Important" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<!-- ...but not the rest of it -->
<loggerToMatch value="Noisy.Namespace" />
<acceptOnMatch value="false" />
</filter>
A filter certainly works but I would prefer 6 to turn off the logger (or logger hierarchy) directly 5 like this:
<logger name="YourNameSpace.WithNoLogging" additivity="false">
<level value="OFF" />
</logger>
<logger name="MyClass" additivity="false">
<level value="OFF" />
</logger>
<root>
<level value="ALL" />
<appender-ref ref="YourAppender" />
</root>
Assuming that YourNameSpace.WithNoLogging
is a namespace 4 then the shown configuration would disable 3 logging on the entire name space. The second 2 "example" turns off logging for your class 1 (according to your question).
I would suggest using Filters as well. However, since 16 I struggled finding the whole picture when 15 I was trying to implement the filter I am 14 posting a sample snippet of the Configutation file
I created 13 which points out where filters go.
The filter 12 you are going for in this case would be
log4net.Filter.LoggerMatchFilter 11 ----(Matches against a the start of the logger 10 name.)
Hint: In the config
file for Log4Net it is important 9 where you put your tags and the priority 8 of them actually matters. So in this case 7 <filter>
tag comes after the <appender>
opening tag and before 6 it's <file value = ... />
tag.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFile.PassedDevices" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Foo.namespace.bar.mySubclass" />
<acceptOnMatch value="false" />
</filter>
<file value="myPassedDevices.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%timestamp %level - %message [%thread] %logger%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" /> <!-- My other appender which logs all and I cut it out in this snippet. Remember that you should reference all your appenders in this tag to make them work.-->
<appender-ref ref="RollingFile.PassedDevices" />
</root>
</log4net>
</configuration>
In this technique you can have 5 multiple appenders
which you can redirect the logging 4 results of a specific logger to a separate 3 appender
instead of ignoring them. Such as one appender
for 2 all logs and one for the filtered out logs 1 for a specific class
.
You might want to try to apply a category 1 to your own messages Try this thread: How to add category prefix to log4net message?
Just wanted to post the common exclusions 2 if you are using NHibernate and log4net. Note 1 that each filter needs its own element.
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NHibernate.Engine.StatefulPersistenceContext.ProxyWarnLog" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NHibernate.LazyInitializationException" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NHibernate.Cache.NoCacheProvider" />
<acceptOnMatch value="false" />
</filter>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.