Matthew Czarnek Matthew Czarnek -4 years ago 157
C# Question

Log4net Root logger working, other one not

I have the following log4net configuration. For some reason the "root" logger works, which the "logger" logger does not. I have tried switching the contents of the two and the other one will start working while whichever one is in "logger" does not.

The log4net Debugging does not appear to be outputting anything...? Checked console which is where I hear it's supposed to be output to.

Any advice?

Thank you!!

<?xml version="1.0" encoding="utf-8" ?>
<add key="log4net.Internal.Debug" value="true"/>
<section name="log4net"
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<logger name="ConsoleLogger">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />

The code I am using in the C# is:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]

private static readonly ILog Logger = LogManager.GetLogger(typeof(Feeder));

Logger.Info("Message to Log");

Answer Source

Your problem is that you have created a named logger - ConsoleLogger - but then you are resolving a log object based on your Feeder type name.

Because there is no defined logger called E.G.Namespace.Feeder, log4net falls back to the root logger, which is what you have seen happening.

In order to use the ConsoleLogger you have to ask for it by name:

private static readonly ILog Logger = LogManager.GetLogger("ConsoleLogger");

(Or alternatively rename it in the config file)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download