Log4Net issue while using RollingFileAppender

1.6k Views Asked by At

I am using log4net to perform logging in my application. I have bound my project to TFS. I have created a wrapper around log4net as below:

public static class TestLogger
{
    private static readonly ILog log = LogManager.GetLogger("TestLogger");

    static TestLogger()
    {
        log4net.Config.XmlConfigurator.Configure();
    }

    public static void LogInfo(string information)
    {
        log.Info(information);
    }

    public static void LogError(string erroMessage, Exception ex)
    {
        log.Error(erroMessage, ex);
    }

    public static void LogWarnings(string warningText)
    {
        log.Warn(warningText);            
    }
}

When I tried to execute the program from VS2010 I found that log file is not being created. I create another project (not bound to TFS) and perform some logging, it succeeded and created the file in bin/debug of application.

Below is my log4net configuration file.

<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
    <file value="Log.txt" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="3" />
    <maximumFileSize value="1GB" />
    <layout type="log4net.Layout.PatternLayout, log4net">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <logger name="TestLogger">
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender" />
  </logger>
</log4net>

Can anybody help in this issue?

4

There are 4 best solutions below

0
On

Try to turn on internal debugging as explained here. This should tell you what the problem is. If there is no output from internal debugging then you probably did not configure log4net.

1
On

Some troubleshooting tips:

  1. define an absolute path to the Log file in your config file.

  2. check the current working directory in your code (Environment.CurrentDirectory). If you're running under the VS debugger, and you haven't specified a working directory in the Debug tab of your project properties, it may well default to the current Visual Studio working directory.

I don't think being bound to TFS is relevant.

0
On

Maybe your application already uses some declarative configuration, which is somewhere burried in the code. Search for something like this: [assembly: log4net.Config.XmlConfigurator(Watch=true)]

Otherwise try to get hold of the log4net repository with log4net.LogManager.GetRepository(). It returns an object of the type ILoggerRepository. You can try to use this object to write some information about the current log4net configuration into the Console or somewhere else.

0
On

Just change this part

<appendToFile value="true" />