You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Ross Hinkley <ro...@gmail.com> on 2008/06/13 21:41:45 UTC

filtering on methods

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

Re: filtering on methods

Posted by Ross Hinkley <ro...@gmail.com>.
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

On Fri, Jun 13, 2008 at 2:53 PM, Rob Prouse <Ro...@ivara.com> wrote:

>  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
>
>
>
> *From:* Ross Hinkley [mailto:rosshinkley@gmail.com]
> *Sent:* June-13-08 3:42 PM
> *To:* log4net-user@logging.apache.org
> *Subject:* filtering on methods
>
>
>
> 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
>

RE: filtering on methods

Posted by Rob Prouse <Ro...@Ivara.com>.
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

From: Ross Hinkley [mailto:rosshinkley@gmail.com]
Sent: June-13-08 3:42 PM
To: log4net-user@logging.apache.org
Subject: filtering on methods

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

Re: filtering on methods

Posted by Peter Drier <pe...@gmail.com>.
<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

On Fri, Jun 13, 2008 at 3:41 PM, Ross Hinkley <ro...@gmail.com> wrote:

> 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
>