Search code examples
c#logginglog4netlog4net-appender

Log4net appenders overwritten by another process


I have an application that is using log4net. However when I show a SaveFileDialog, the appenders get overwritten by another process. Looking at the new appenders, I believe this is because they are from a process dealing with icon overlays (since the log file they use is called "icon-overlays.log") and I have opened explorer within my application.

Example:

Log.Fatal("Before dialog");// logs fine

SaveFileDialog newFileDialog = new Microsoft.Win32.SaveFileDialog();

Log.Fatal("Open dialog");//logs fine

var appenders1 = Log.Logger.Repository.GetAppenders();//shows appenders from my config

if (newFileDialog.ShowDialog() == false)
{
    Log.Fatal("Cancelled dialog");//does not log
    return;
}

var appenders2 = Log.Logger.Repository.GetAppenders();//shows appenders from other process
Log.Fatal("New database selected");//does not log

When I find the log files from these new appenders I can see that logs I am missing are going there.

Does anyone know how this is happening, and how to avoid it?


Solution

  • You have to look in the log4net debug log to see what is going on. It looks like you have multiple places where you configure/reconfigure log4net and hit such place when you open the dialog.

    Enable internal debug:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    </configuration>
    

    Write to a specific file:

    <configuration>
        ...
    
        <system.diagnostics>
            <trace autoflush="true">
                <listeners>
                    <add 
                        name="textWriterTraceListener" 
                        type="System.Diagnostics.TextWriterTraceListener" 
                        initializeData="C:\tmp\log4net.txt" />
                </listeners>
            </trace>
        </system.diagnostics>
    
        ...
    </configuration>