ilitirit
ilitirit

Reputation: 16352

Logging framework for .NET that supports multiple applications writing to the the same rolling log file?

I have a console application that writes to a rolling log file. The problem is that application can be called by several processes and there's a chance that it will be called simultaneously. The application writes to one log file. I'm currently investigating using log4net with the MinimalLock setting:

<lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />

but I've read that issues can arise if a logging message from one instance causes a roll over while another instance is trying to write to the same file.

Is there another framework that supports this behaviour, or perhaps a suitable workaround?

Upvotes: 0

Views: 665

Answers (1)

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

try <lockingModel type="log4net.Appender.FileAppender+InterProcessLock"

Now following code will be thread safe because you will create a logger static class to log information.

public static class Logger
{
    private static readonly Object obj = new Object();

    private static ILog _appLog = null;
    static Logger()
    {
        XmlConfigurator.Configure();
    }

    public static void Log(string loggername)
    {
        lock (obj)
        {
            _appLog = LogManager.GetLogger(loggername);
            _appLog.Error(....);
        }
    }
}

Upvotes: 1

Related Questions