Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net File Appender Not Logging

Tags:

.net

log4net

my FileAppender isn't working. It probably isn't a permissions thing as I'm local admin.

I've enabled internal debugging and I'm not getting any errors or exceptions from log4net.

My config file is below. Thanks

<log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
            </layout>
        </appender>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <file value="C:\log-file.txt" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="ConsoleAppender" />
            <appender-ref ref="FileAppender" />
        </root>
    </log4net>

The code to log is effectively:-

public class Logger
{
    private static readonly ILog defaultLog;
    static Logger()
    {
        BasicConfigurator.Configure();
        defaultLog = LogManager.GetLogger("default");
    }

    public static void Log(string errorMessage, Exception exception)
    {
        defaultLog.Error(errorMessage, exception);
    }
}
like image 744
BenCr Avatar asked Dec 10 '22 04:12

BenCr


2 Answers

Based on your description I assumed, that you used the BasicConfigurator (supports only ConsoleAppender) instead of the XmlConfigurator. Switching to the XmlConfigurator should fix your problem.

like image 128
Stefan Egli Avatar answered May 25 '23 05:05

Stefan Egli


There are 2 main options to enable logging. Refer to the Configuration manual for more details.

  1. Use XmlConfigurator.Configure()
  2. Add [assembly: log4net.Config.XmlConfigurator(Watch=true)] to AssemblyInfo.cs

BasicConfigurator.Configure() can only write to the console.

like image 43
Vassili Altynikov Avatar answered May 25 '23 07:05

Vassili Altynikov