Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net: Configure to ignore messages from a specific class

Is there a way to have the log4net configuration ignore a specific class? For example, we generally create a log in every class. Similar to this:

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

The problem is MyClass logs an extreme amount of data and it gets hard to find information about other classes. Its another dev that uses MyClass so I cannot just go in and change around the log files, but in my environment I would like to ignore these.

Can I set my configuration file to ignore the messages from a specific class?

like image 510
Kyle Avatar asked Mar 31 '11 17:03

Kyle


2 Answers

Sure, use a filter.

Here's the snippet posted on the blog, for future reference - all credit 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>
like image 182
jvenema Avatar answered Nov 19 '22 10:11

jvenema


A filter certainly works but I would prefer to turn off the logger (or logger hierarchy) directly 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 then the shown configuration would disable logging on the entire name space. The second "example" turns off logging for your class (according to your question).

like image 60
Stefan Egli Avatar answered Nov 19 '22 09:11

Stefan Egli