Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net with ASP.NET 3.5 problems

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.

like image 750
Rob Avatar asked Oct 22 '08 03:10

Rob


People also ask

Does log4net work with .NET 5?

log4net is one of three very popular frameworks for implementing log messages in your application (Serilog and NLog being the other two). log4net works with almost any version of . NET (including . NET Core).

Does log4net support .NET Core 6?

Yes, you can still use Log4Net to help you output log statements of text files, databases, and other destinations using . NET Core 6 by installing log4net version 2.0. 7 (this version targets . NET Standard) and beyond via NuGet Packages.

Does log4net support .NET core?

NET Standard (2.0. 7 and beyond). In fact, I can use Log4Net alongside the default logging API for . NET Core: Microsoft.

Does .NET framework use log4j?

The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® . NET runtime.


1 Answers

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.

like image 86
CVertex Avatar answered Oct 02 '22 15:10

CVertex