Reputation: 14148
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
Upvotes: 0
Views: 3213
Reputation: 236208
Configuration, which you provided should work.
When you create logger Com.Foo.Bar
it inherits settings from Com.Foo
logger in hierarchy. Com.Foo
logger inherits his appenders from root logger, but it has own level, which is set to WARN
. So, when you trying to write logging event via Com.Foo.Bar
logger, effective level will be retrieved from hierarchy - it's a level of nearest logger up in the hierarchy tree (root logger always has level). In your case it is WARN
, so logging event will not be passed to appenders.
I think your configuration differs from what you provided. Maybe you are reading wrong configuration file. Try this code (app configuration file will be used):
XmlConfigurator.Configure();
Or (even better) use configuration via attribute:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
UPDATE
If changing logger retrieving from typeof(Bar)
to "Com.Foo"
worked, then you provided us wrong namespace of Bar class. Because log4net behind the scene takes full name of type as name of logger. Thus with namespace Com.Foo
all should work.
Upvotes: 1
Reputation: 14148
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.
Upvotes: 0