Zee Fer
Zee Fer

Reputation: 339

unable to get log4net to write to file

I've been trying to pull the debug logs and for whatever reason, I can't seem to get log4net working in my web app:

My Web.config looks like:

<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->


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

    <log4net>
      <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value ="%message" />
        </layout>
      </appender>

      <appender name="FileAppender" type="log4net.Appender.FileAppender" >
        <file value ="C:\logs\testlog.txt" />
        <lockingModel type ="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value ="%message" />

        </layout>
      </appender>

      <root>
        <level value ="DEBUG" />
     <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="FileAppender" />
      </root>
    </log4net> 

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>

in my Service.asmx.cs:

public class Service : System.Web.Services.WebService
{       

   public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Service));

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

My AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Control.ascx.cs:

protected void Page_Load(object sender, EventArgs e)
{
        //NOTE: This method will be invoked whenever ANY widget on the dashboard does a postback
        log4net.Config.XmlConfigurator.Configure();
    }

I have given everyone permissions to the log folder.

The other item I wanted to note is that I can get this exact code to work with a standard console app without any issues. However, when I try to use it on my web app, I do not have any luck. I also wanted to say that this is a web app widget that is part of a bigger application.

Upvotes: 0

Views: 273

Answers (1)

Peter
Peter

Reputation: 27944

One of your problems can be that you have the following in the page load:

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

Remove this because you already have a assembly attribute that does this. The loggers you are using are static variables on the classes. They should be initialized after you call configure. In this case you call it before configure (which is in Page_Load).

Upvotes: 1

Related Questions