views:

225

answers:

3

I'm trying to set up log4net but I cannot make it work. I've put this in my Web.config:

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logfile.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="14" />
    <maximumFileSize value="15000KB" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="TraceAppender" />
  </root>
</log4net>

Then, in my code I execute:

log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath("~/Web.config")));
ILog log = LogManager.GetLogger("MainLogger");

if (log.IsDebugEnabled)
    log.Debug("lalala");

But nothing happens. I checked the "log" variable, and it contains a LogImpl object that has all the logging levels enabled. I get no error or configuration warning, I cannot see any file in the root, in the bin or anywhere.

What do I have to do to make it work?

+2  A: 

Do you have this line in your AssemblyInfo.cs file?

[assembly: log4net.Config.XmlConfigurator()]

Also, you should consider the "typeof(YourClass)" approach used by Eric in his answer below. The first time I have upvoted an alternative answer to one of mine. :)

You don't have a TraceAppender defined.

You don't have a logger named "MainLogger" configured

<logger name="MainLogger">
  <level value="DEBUG"/>
  <appender-ref ref="RollingFileAppender" />
  <appender-ref ref="TraceAppender" />
</logger>

Also, have a look here

Daniel Dyson
But he does have a <root> defined, which is a catch-all/default.
Hans Kesting
Fair point. I'm in the habit of being quite specific. My answer is incorrect and I will update it as soon as I find the correct answer
Daniel Dyson
Adding that, I get the lines duplicated, but if I remove the root, then it works fine! :PThanks!
vtortola
That is good. Just remember that you will now have to always call that logger explicitly.
Daniel Dyson
A: 

You will need another method to configure log4net: log4net.Config.XmlConfigurator.Configure().

Then it will read the web.config automatically.

See http://logging.apache.org/log4net/release/manual/configuration.html, under the ".config files" section.

Hans Kesting
+5  A: 

Try to write:

log4net.Config.XmlConfigurator.Configure();

instead, since Web.config is the default location where Log4Net will look.

Otherwise, remove that entire line from your code and paste the following into your AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator()]

This will configure Log4Net at assembly level. Then in your code, just create a logger like this:

private static readonly ILog Log = LogManager.GetLogger(typeof(YourFunkyClass));
Eric Eijkelenboom
+1 I often use this syntax
Daniel Dyson