4
votes

There are a lot of questions/answers regarding configuring log4net and I have been looking at them for a while. Time to ask my own question. My problem is that my "Web api" web service does not create log files. I want to create log files at: C:\Logs. I have double checked that I have permission to write to this folder. In addition, we use log4net for our functional tests and when I run the tests, the file FunctionalTests.log is created. Therefore, I think we can rule out permissions.

What am I doing wrong here?

I have this line in my AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

I call XmlConfigurator.Configure() when my application starts. In my debugger I can see that Application_Start is hit and the Configure() method invoked:

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        XmlConfigurator.Configure();
        HibernateConfig.InitHibernate();
    }

Here is my Log4Net.config file, located at the root level of my project:

<log4net debug="true">
  <!-- Level 1 -->
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="C:\Logs\WebAPI.log"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="2" />
    <maximumFileSize value="1000MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
     <param name="ConversionPattern" value="%d %property{X-CLIENT-REQUEST-ID} %property{X-REQUEST-ID} [%t] %-5p %c %m%n"/>
    </layout>
  </appender>

  <appender name="RequestLogAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="C:\Logs\WebAPIRequests.log"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="2" />
    <maximumFileSize value="1000MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d %property{X-CLIENT-REQUEST-ID} %property{X-REQUEST-ID} [%t] %-5p %c %m%n"/>
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <logger additivity="false" name="Request">
    <level value="INFO" />
    <appender-ref ref="RequestLogAppender" />
  </logger>
</log4net>
2
Aside: the assembly attribute isn't necessary if you already explicitly call Configure, and, in fact, because it's so unreliable as to when (if) log4net can process it, I recommend not relying on it altogether. Just call XmlConfigurator.ConfigureAndWatch() from your Application_Start.Jeroen Mostert
Thanks for the comment. I have removed this.thebiggestlebowski
@JeroenMostert Your comment should be the answer, and you made my day! I could not figure out why log4net didn't work in IIS, it was all fault of the assembly attribute, definitely using XmlConfigurator.ConfigureAndWatch() from Application_Start should be the way to goAlberto Rechy

2 Answers

6
votes

The problem was that visual studio wasn't copying my log4net.config file to the bin directory. To fix: right click on log4net.config -> properties -> copy always.

I also had a second problem to fix. My configuration file wasn't read, even when under the bin directory. I edited my Web.config file and added these two lines:

<appSettings>
    <add key="log4net.Config" value="Log4Net.config"/>
    <add key="log4net.Config.Watch" value="True"/>
</appSettings>
0
votes

Better late than never =]

Follow my configuration on web.config

    <log4net debug="false">
      <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
        <param name="File" value="log/log" />
        <param name="AppendToFile" value="true" />
        <param name="rollingStyle" value="Composite" />
        <param name="datePattern" value="yyyyMMdd" />
        <param name="MaximumFileSize" value="100MB" />
        <param name="MaxSizeRollBackups" value="-1" />
        <param name="StaticLogFileName" value="false" />
        <layout type="log4net.Layout.PatternLayout,log4net">
           <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
        </layout>
      </appender>
      <root>
        <priority value="ALL" />
        <appender-ref ref="LogFileAppender" />
      </root>
    </log4net>

In my case I created a static "Log Helper" to centralize all logs call

And I used the static contructor to configure the log4net behavior , as follows :

public class LogHelper
{
    private static readonly ILog Log;

    static  LogHelper()
    {
        Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        log4net.Config.XmlConfigurator.Configure();
    }

    public static void RegistrarLogInfo(string message)
    {
        Log.Info(message);
    }

    public static void RegistrarLogError(string message)
    {
        Log.Error(message);
    }
}

Hope it works