Rectangle 27 1

By default the FileAppender holds an exclusive write lock on the log file while it is logging. This prevents other processes from writing to the file. The FileAppender can be configured to use a different locking model, MinimalLock, that only acquires the write lock while a log is being written. This allows multiple processes to interleave writes to the same file, albeit with a loss in performance. See the FileAppender config examples for an example MinimalLock configuration.

While the MinimalLock model may be used to interleave writes to a single file it may not be the optimal solution, especially when logging from multiple machines. Alternatively you may have one or more processes log to RemotingAppenders. Using the RemoteLoggingServerPlugin (or IRemoteLoggingSink) a process can receive all the events and log them to a single log file.

Without additional configuration, log4net puts an exclusive lock on the file. Using their MinimalLock setting, you can "hopefully" get shared logging. In other words, your mileage may vary.

As a suggested alternative strategy, since you've got an internal API with web services, consider a web service method that implements a single private static logger in the background and adds entries to the log. You could call the logging web method (asynchronously if performance is critical) from your other web methods where you want to implement logging.

logging - One log file and one error handler for set of web services -...

web-services logging error-handling log4net
Rectangle 27 0

I've never used FileAppender and cannot say if it is thread safe but I have never had any problems with RollingFileAppender. The docs state that members of the type are not thread safe, but this should be OK unless you try to directly write to the appender. You do not need to add your own locking code around calls like:

log.Info("message");

So you're saying that Log4Net is multithread safe if it's used through the Logger API? So I guess there has to be problem in our code that's using it?<br> In each class we are creating a logger like this: <pre> <code> private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); </code> </pre><br> But only one instance of this class is used in a thread.

c# - Multithread safe logging - Stack Overflow

c# .net multithreading logging log4net
Rectangle 27 0

Multiple FileAppender with minimal locking

not a good idea to use multiple RollingFileAppender instances from different processes

You have a couple of choices here:

Use multiple FileAppender instances pointing at the same file and configured with minimal locking. This will allow concurrent write operations from multiple processes:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="Log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

Use multiple EventLogAppender instances that write to a shared Windows event source:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <applicationName value="MyApp" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

Of course you could also send the log to a database or to a remote component running in a different process or even a different machine but these choices require more infrastructure to be setup so it may be overkill for your scenario.

c# - Logging multiple instance application best practice? - Stack Over...

c# .net log4net multiple-instances