Loggers have a Level property that they use to filter logging events
before they route the event to the appender. The Level is inherited from
their parent Logger unless specified.
You don't want to set the Threshold on the appender unless you never
want to see events below that threshold in the output.
Try this:
static void Main(string[] args)
{
Logger a = LogManager.GetLogger("a").Logger as Logger;
Logger ab = LogManager.GetLogger("a.b").Logger as Logger;
Logger ac = LogManager.GetLogger("a.c").Logger as Logger;
ConsoleAppender ca = new ConsoleAppender();
ca.Name = "Test";
ca.Layout = new SimpleLayout();
ca.ActivateOptions();
a.AddAppender(ca);
a.Repository.Configured = true;
// Set level on logger 'a'. This will be inherited by other loggers
a.Level = Level.ERROR;
// Override the level on 'a.b'. Set it to Debug
ab.Level = Level.DEBUG;
ILog abLog = LogManager.GetLogger("a.b");
abLog.Debug("Test Error message for ab");
ILog acLog = LogManager.GetLogger("a.c");
acLog.Warn("Test Error message for ac");
}
> -----Original Message-----
> From: Shaily Goel [mailto:[EMAIL PROTECTED]
> Sent: 02 September 2005 12:29
> To: [email protected]; Nicko Cadell
> Subject: RE: inheritance of appenders from Parent
>
> Thanks For quick reply.
>
> But my requirement is different. I want to do Component-Level
> Logging which means I have root logger and various child
> loggers of it.
> For a particular logger I want to turn Debug Level on for
> Console Appender Whereas rest should work as it is. How
> should I do that programmatically
>
> static void Main(string[] args)
> {
> Logger a = LogManager.GetLogger("a").Logger as Logger;
>
> a.Additivity = true;
> a.Repository.Configured = true;
>
>
> Logger ab = LogManager.GetLogger("a.b").Logger as Logger;
>
>
> Logger ac = LogManager.GetLogger("a.c").Logger as Logger;
>
>
> ConsoleAppender ca = new ConsoleAppender();
> ca.Name = "Test";
> ca.Layout = new SimpleLayout();
> ca.Threshold = Level.ERROR;
> ca.ActivateOptions();
>
> a.AddAppender(ca);
>
> ConsoleAppender ar =
> (ConsoleAppender)FindAppender(ab,"Test");
> ar.Threshold = Level.DEBUG;
>
> ILog abLog = LogManager.GetLogger("a.b");
> abLog.Debug("Test Error message for ab");
>
> ILog acLog = LogManager.GetLogger("a.c");
> acLog.Warn("Test Error message for ac");
> }
>
>
> I should not be able to log Warning Message for "a.c".
>
> But I found that Console Appender show both Debug and
> Warning message. How can I avoid this?
>
> Thanks
> Shaily
>
>
> >>> [EMAIL PROTECTED] 09/02/05 4:27 PM >>>
>
> The Logger.GetAppender method does not get inherited
> appenders. It only returns appenders defined on the Logger istelf.
>
> To get all the appenders you need to look at all the parent loggers as
> well:
>
> Appender FindAppender(Logger log, string appenderName) {
> for(Logger l=log; l!=null; l=l.Parent)
> {
> Appender a=l.GetAppender(appenderName);
> if (a != null)
> {
> return a;
> }
> if(!l.Additivity)
> {
> break;
> }
> }
> return null;
> }
>
> Cheers,
> Nicko
>
> > -----Original Message-----
> > From: Shaily Goel [mailto:[EMAIL PROTECTED]
> > Sent: 02 September 2005 10:58
> > To: [email protected]; Nicko Cadell
> > Subject: inheritance of appenders from Parent
> >
> > Hi
> >
> > I am facing a problem in inheritance of appenders from Parent.
> >
> > version of Log4net used: incubating-log4net-1.2.9-beta ;
> > Platform: Windows
> >
> > Test: Test if logger a.b inherits its appender from a.
> >
> > static void Main(string[] args)
> > {
> >
> > Logger a = LogManager.GetLogger("a").Logger as Logger;
> > a.Additivity = true;
> > a.Repository.Configured = true;
> >
> > ConsoleAppender ca = new ConsoleAppender();
> > ca.Name = "Test";
> > ca.ActivateOptions();
> >
> > a.AddAppender(ca);
> > Logger ab = LogManager.GetLogger("a.b").Logger
> as Logger;
> > IAppender ar = ab.GetAppender("Test"); }
> >
> > I found that there is no appender inherited by Logger a.b.
> > Why is it so?
> >
> > What should I do to make "a.b" inherits all the appender of "a".
> >
> > Thanks
> > Shaily
> >
> >
> >
>
>