log4net not working

Viewed 84742

Hey I have this configuration in my web.config

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
        <param name="File" value="mylog.log" />
        <param name="AppendToFile" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="Header" value="" />
            <param name="Footer" value="" />
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
        </layout>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <param name="Header" value="[Header]\r\n" />
            <param name="Footer" value="[Footer]\r\n" />
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="ConsoleAppender" />
    </root>
</log4net>

but log4net is not working. My project compiles fine, and I get no errors debugging either. The lines where I tell to log.debug("somemessage") gets run fine, but I can't find the mylog.log file, so where is it?

16 Answers

On my side, I forgot to mark the config file to be copied while the app is being compiled.

Copy the config file to output directory

Just right click the log4net.config file, select property and then choose Copy to Output Directory to be Copy XXXX

Unfortunately none of the above helped. Explicit configuration in the class to be logged additionaly to the previous settings suggestions did the trick for me.

string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
log4net.Config.XmlConfigurator.Configure(new FileInfo(assemblyFolder + "/log4net.config"));

So, in the Accepted Answer, the solution was to put

[assembly: log4net.Config.XmlConfigurator]

into the AssemblyInfo.cs File.

Also, there was a comment sugesting

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

however, if it is still not working, consider configuring the name of your log4net config file (log4net.config in my case):

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

after that, logging worked like a charm.

In my case I forget to set the log4Net.config file properties as "Content" so the file was not included in the deploy. So pay attention on it:

Compile action : Content

Check your Reporting level is set to "Debug" or "ALL". This caught me and I spent ages trying to debug my log4net setup. I had it set to Error and as it wasn't erroring it didn't log anything.

Aside from all the other answers, my problem that was I needed a call to GetLogger() in my start-up class (WinForms example):

static class Program
{
    public static readonly ILog MainLog = LogManager.GetLogger("Main");

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

All my 'meaningful' logging statements were in a DLL that is apparently loaded too late for log4net.

Related