Friday, May 18, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: filtering on methods
Prev Next
You are not authorized to post a reply.

Author Messages
Ross Hinkley

06/13/2008 9:41 PM  
This might seem like I'm a little log4net-challenged (and that may well be), but after spending some time with the message archives and Google, I could not figure this one out.  So, I turn to you lot for a hand.

Is there a quick and simple way to filter messages based on the method, class, or namespace?

I'm thinking of a situation where you have class A using class B in a has-a relationship, where class B is just a helper class.  For debugging purposes, having output from class B is well and good, but it generates a surfeit of data.  Say I still want to debug class A without having log information for class B.  How would I go about doing that?

Thanks in advance,
Ross
Peter Drier

06/13/2008 9:53 PM  
<logger name="Namespace.ClassB" >
   <level value="ERROR" />
</logger>

will filter out any log messages less than ERROR level from ClassB..

So assuming you have something like this already:
        <root>
            <level value="DEBUG" />
            <appender-ref ref="DefaultAppender" />
        </root>

Combined with the above, you'll get Debug and higher from everything except ClassB.

-Peter

--
Peter's Photography
www.PeterDrier.com

Rob Prouse

06/13/2008 9:53 PM  

It doesn’t work down to the method level, but most people add a static ILog per class based on the class name. You would then set up your configuration to only log you’re class A.

 

Here is a very untested example of code and config that logs all warnings and errors, but just debug info from class A;

 

namespace Blah

{

   public class A

   {

      private static readonly ILog log = LogManager.GetLogger( typeof( A ) );

     

      public void Foo()

      {

         log.Info( "Log Something" );

      }

   }

 

   public class B

   {

      private static readonly ILog log = LogManager.GetLogger( typeof( B ) );

     

      public void Bar()

      {

         log.Info( "Log Something Else" );

      }

   }

}

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

   <configSections>

      <section name="log4net"

        type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

   </configSections>

   <log4net>

      <appender name="FileAppender" type="log4net.Appender.FileAppender">

         <layout type="log4net.Layout.PatternLayout">

            <ConversionPattern value="%d [%t] %-5p %P %c %m%n" />

         </layout>

      </appender>

      <root>

         <level value="WARN" />

         <appender-ref ref="FileAppender" />

      </root>

      <logger name="Blah.A">

         <level value="DEBUG" />

      </logger>

   </log4net>

</configuration>

 

Hope this helps. If you want to go down to the method level, I would suggest adding the method name into your logging string and filtering on that. You could create a logger per method, but that is overkill in my opinion. Anyone else have a better suggestion for the method level?

 

Rob Prouse

 

Ross Hinkley

06/13/2008 10:00 PM  
Peter, Robert-

Thank you for your quick responses!!  Peter's solution seems to be the ticket I was looking for.  Thank you both for your time.  I guess what makes me feel a little exasperated is that I've read over similar sections a dozen times today, looking down a completely wrong path.

Thanks for being my second set of eyes.

-Ross

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > filtering on methods



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement