Search code examples
c#asp.netlog4netconsoleappender

log4net configuration for a given class not working


namespace Com.Foo
{
    public class Bar
    {

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

        public void DoIt()
        {
            log.Info("Did it again!");
        }
    }
}

class Program
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Program));
    static void Main(string[] args)
    {
        string sfile = @"C:\development\Framework\Logging\ConsoleApplication1\app.config";
        XmlConfigurator.Configure(new System.IO.FileInfo(sfile));
        log.Info("Entering application.");
        Bar bar = new Bar();
        bar.DoIt();
        log.Info("Exiting application.");

        Console.ReadLine();
    }


}

My log4net configuration looks as follows:

<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">

  <!-- A1 uses PatternLayout -->
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
  </layout>
</appender>

<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
  <level value="DEBUG" />
  <appender-ref ref="A1" />
</root>


<!-- Print only messages of level WARN or above in the package Com.Foo -->
<logger name="Com.Foo">
  <level value="WARN" />
</logger>

The output of my application still shows log from Com.Foo

67 [10] INFO ConsoleApplication1.Program (null) - Entering application.

100 [10] INFO ConsoleApplication1.Com.Foo.Bar (null) - Did it again!

100 [10] INFO ConsoleApplication1.Program (null) - Exiting application.

How do I configure such that Com.Foo.Bar stops from showing up for WARN level?

What am I missing?

Thanks


Solution

  • I just figured it out.. I was not setting the logger properly in Foo.Bar class.

    Update the following line

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

    to the following..

    private static readonly ILog log = LogManager.GetLogger("Com.Foo");
    

    and it worked.