Log4net: configure ignoring messages of a specific class

Is there a way for the log4net configuration to ignore a specific class? For example, we usually create a journal in each class. Similar to this:

private static readonly ILog Log = log4net.LogManager.GetLogger("MyClass"); 

Problem MyClass registers an extreme amount of data, and it is difficult to find information about other classes. Its another developer who uses MyClass , so I can't just log in and edit the log files, but in my environment I would like to ignore them.

Can I install a configuration file to ignore messages from a specific class?

+60
c # logging log4net
Mar 31 '11 at 17:53
source share
4 answers

Of course, use a filter.

Here's a snippet posted on the blog for future reference - all that belongs to the author of this 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> 
+69
Mar 31 '11 at 17:55
source share

The filter certainly works, but I would prefer to disable the registrar (or registrar hierarchy) in the following way:

 <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 YourNameSpace.WithNoLogging is a namespace, this configuration disables logging in the entire namespace. The second β€œexample” disables logging for your class (according to your question).

+52
Mar 31 '11 at 21:00
source share

I would suggest using Filters . However, since I was trying to find the whole picture, when I was trying to implement a filter, I send a sample fragment created by the Configutation file , which indicates where the filters go.

The filter you are going to in this case will be

log4net.Filter.LoggerMatchFilter ---- (Same as the beginning of the logger name.)

The hint in the config file for Log4Net is important when you add tags and their priority actually matters. Thus, in this case, the <filter> tag appears after the <appender> and before it is the <file value = ... /> .

 <?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 method, you can have several appenders that you can redirect the logging results of a particular registrar to a separate appender instead of ignoring them. For example, one appender for all logs and one for filtered logs for a particular class .

+14
Jun 05 '14 at 23:20
source share

You might want to apply a category to your own posts. Try this topic: How do I add a category prefix to a log4net message?

+2
Mar 31 '11 at 18:00
source share



All Articles