David Shochet
David Shochet

Reputation: 5375

Log4net is not logging

I have a small console application, which refers to a library project. I am trying to log possible errors using log4net. It works when I call e.g. Log.Debug(myErrorMsg) from the main project, but when I try to do the same from the library project, it writes nothing. What could be the cause of this?

In my app.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
    <root>
       <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="C:\MyProject\bin\Debug\log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
</log4net>

In my main():

XmlConfigurator.Configure(); 

In my dll project:

internal static readonly ILog Log = LogManager.GetLogger(typeof(FileWatcherLogic));

...

catch (Exception ex)
{
    var msg = string.Format("Error handling file {0}: {1}", fullPath, ex.Message);
    Console.Write(msg);
    Log.Error(msg);
}

Upvotes: 0

Views: 318

Answers (1)

Mike Corcoran
Mike Corcoran

Reputation: 14565

It looks like you're missing the <level> element in your <root> declaration to tell log4net which log levels to actually log.

Does changing your current root to this fix your problem?

<root>
   <level value="ALL" /> <!-- or "DEBUG", "WARN", "INFO", etc -->
   <appender-ref ref="LogFileAppender" />
</root>

Alternatively, if that doesn't work - you can add this key to your app or web.config in your <appSettings> to get verbose debugging information written to the console by log4net to tell you exactly what is wrong:

<add key="log4net.Internal.Debug" value="true"/>

Upvotes: 1

Related Questions