log4net with ASP.NET 3.5 problems

Rob picture Rob · Oct 22, 2008 · Viewed 15.6k times · Source

I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the first time I've tried to use log4net, I feel like I'm missing a piece of the puzzle.

My project references the log4net assembly, and as far as I can tell, it is being deployed successfully on my server.

My web.config contains the following:

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

  <log4net>
    <appender name="InfoAppender" type="log4net.Appender.FileAppender">
      <file value="..\..\logs\\InfoLog.html" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
          value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>
    <logger name="_Default">
      <level value="INFO" />
      <appender-ref ref="InfoAppender" />
    </logger>
  </log4net>

I'm using the following code to test the logger:

using log4net;
using log4net.Config;

public partial class _Default : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetLogger("_Default");

    protected void Page_Load(object sender, EventArgs e)
    {
        log.Info("Hello logging world!");
    }
}

In my Global.asax, I'm doing the following:

void Application_Start(object sender, EventArgs e) 
{
    log4net.Config.XmlConfigurator.Configure();
}

At this point, I can't think of what else I might be doing wrong. The directory I'm trying to store the log in is writable, and even if I try different directories I get the same result: no file, no logs.

Any suggestions? :-)


Edit: I've tried several different formats for the path & name of the log file, some of which include "..\..\InfoLog.html", "InfoLog.html", "logs\InfoLog.html", etc, just in case someone is wondering if that's the problem.


Edit: I've added the root logger node back into the log4net section, I ommitted that on accident when copying from the samples. The root logger node looks like this:

<root>
  <level value="INFO" />
  <appender-ref ref="InfoAppender" />
</root>

Even with it, however, I'm still having no luck.

Answer

CVertex picture CVertex · Oct 22, 2008

The root logger is mandatory I think. I suspect configuration is failing because the root doesn't exist.

Another potential problem is that Configure isn't being pointed to the Web.config.

Try Configure(Server.MapPath("~/web.config")) instead.