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 "Dalton, Barnaby" <Ba...@brevanhoward.com> on 2005/12/05 17:03:10 UTC

filtering on message source

I need to combine messages from one class with messages of level WARN or
above from other classes. I can easily log the WARN and above messages
but was wondering on the best way to add in ALL messages from the
specific class. Is the best technique to define a second logger and then
use LoggerMatchFilter in the appender? Can I avoid having to define a
second logger and use some kind of ClassSource filter?

Thanks
Barney



Re: filtering on message source

Posted by Ron Grabowski <ro...@yahoo.com>.
Here's how to use it:

 <filter type="Company.Project.Logging.CustomFilter">
  <logLevelEqualOrAbove value="WARN" />
  <allMessagesFromLogger value="Company.Project.UI.Login" />
  <allMessagesFromLogger value="Company.Project.UI.Logout" />
 </filter>
 <filter type="log4net.Filter.DenyAllFilter" />

--- Ron Grabowski <ro...@yahoo.com> wrote:

> public class CustomFilter : FilterSkeleton
> {
>  private Level logLevelEqualOrAbove = Level.All;
>  private Hashtable acceptAllMessagesLookup = new Hashtable();
> 
>  public override FilterDecision Decide(LoggingEvent loggingEvent)
>  {
>   if (acceptAllMessagesLookup.Contains(loggingEvent.LoggerName))
>   {
>    return FilterDecision.Accept;
>   }
>   else
>   {
>    if (Level.Compare(loggingEvent.Level,LogLevelEqualOrAbove)>=0)
>    {
>     return FilterDecision.Accept;
>    }
>    else
>    {
>     return FilterDecision.Deny;
>    }
>   }
>  }
> 
>  public Level LogLevelEqualOrAbove
>  {
>   get { return logLevelEqualOrAbove; }
>   set { logLevelEqualOrAbove = value; }
>  }
> 
>  public void AddAllMessagesFromLogger(string logger)
>  {
>   acceptAllMessagesLookup[logger] = null;
>  }
> }


Re: filtering on message source

Posted by Ron Grabowski <ro...@yahoo.com>.
public class CustomFilter : FilterSkeleton
{
 private Level logLevelEqualOrAbove = Level.All;
 private Hashtable acceptAllMessagesLookup = new Hashtable();

 public override FilterDecision Decide(LoggingEvent loggingEvent)
 {
  if (acceptAllMessagesLookup.Contains(loggingEvent.LoggerName))
  {
   return FilterDecision.Accept;
  }
  else
  {
   if (Level.Compare(loggingEvent.Level,LogLevelEqualOrAbove)>=0)
   {
    return FilterDecision.Accept;
   }
   else
   {
    return FilterDecision.Deny;
   }
  }
 }

 public Level LogLevelEqualOrAbove
 {
  get { return logLevelEqualOrAbove; }
  set { logLevelEqualOrAbove = value; }
 }

 public void AddAllMessagesFromLogger(string logger)
 {
  acceptAllMessagesLookup[logger] = null;
 }
}

--- Ron Grabowski <ro...@yahoo.com> wrote:

> It sounds like you need to write a custom filter that looks like
> this:
> 
>  <filter type="Company.Project.Logging.CustomFilter">
>   <logLevelEqualOrAbove level="WARN" />
>   <allMessagesFromLogger name="Company.Project.UI.Login" />
>   <allMessagesFromLogger name="Company.Project.UI.Logout" />
>  </filter>
> 
> That would accept all messages from Login and Logout and only those
> messages from other loggers that are WARN or above.
> 
> Is that correct?
> 
> --- "Dalton, Barnaby" <Ba...@brevanhoward.com> wrote:
> 
> > 
> > I need to combine messages from one class with messages of level
> WARN
> > or
> > above from other classes. I can easily log the WARN and above
> > messages
> > but was wondering on the best way to add in ALL messages from the
> > specific class. Is the best technique to define a second logger and
> > then
> > use LoggerMatchFilter in the appender? Can I avoid having to define
> a
> > second logger and use some kind of ClassSource filter?
> > 
> > Thanks
> > Barney
> > 
> > 
> > 
> 
> 


Re: filtering on message source

Posted by Ron Grabowski <ro...@yahoo.com>.
It sounds like you need to write a custom filter that looks like this:

 <filter type="Company.Project.Logging.CustomFilter">
  <logLevelEqualOrAbove level="WARN" />
  <allMessagesFromLogger name="Company.Project.UI.Login" />
  <allMessagesFromLogger name="Company.Project.UI.Logout" />
 </filter>

That would accept all messages from Login and Logout and only those
messages from other loggers that are WARN or above.

Is that correct?

--- "Dalton, Barnaby" <Ba...@brevanhoward.com> wrote:

> 
> I need to combine messages from one class with messages of level WARN
> or
> above from other classes. I can easily log the WARN and above
> messages
> but was wondering on the best way to add in ALL messages from the
> specific class. Is the best technique to define a second logger and
> then
> use LoggerMatchFilter in the appender? Can I avoid having to define a
> second logger and use some kind of ClassSource filter?
> 
> Thanks
> Barney
> 
> 
>