We have an application that runs in multiple threads and uses Log4Net as logging framework. We encountered a scenario where some log events weren't logged. As mentioned in the docs, the FileAppender and the other Appenders are "not safe for multithreaded operations".
I searched the web for solutions or Appenders, but couldn't find any.
Do you know a multithread safe Log4Net Appender that uses a ring buffer or a queue to provide multithread support? Or should we use a different multithread safe logging framework at all?
Thanks in advance!
All methods on Logger are multi-thread safe.
The logging module can be used directly from multiple threads. The reason is because the logging module is thread-safe.
Multithreading is always faster than serial. Dispatching a cpu heavy task into multiple threads won't speed up the execution. On the contrary it might degrade overall performance. Imagine it like this: if you have 10 tasks and each takes 10 seconds, serial execution will take 100 seconds in total.
I wrote some Unit tests to reproduce the problem: A test creates 50 threads and each thread logs 500 messages. Afterwards the written lines were counted and as a result I got 25,000 (50 x 500) lines in different order. I tested it on a dual core and on a eight core machine.
I tested a static Logger:
private static ILog StaticLog = log4net.LogManager.GetLogger(RepositoryName, "Static logger");
and with a Logger for each instance of the test class / thread:
ILog instanceLog = LogManager.GetLogger(RepositoryName, "Instance logger: " + ThreadId.ToString());
And all tests were green.
So Log4Net works fine and handles multithreading scenarios well. The Appender docs should be updated and state that multithreaded operations are supported if the Logger API is used in the right way.
I guess the problem with missing log entries that we encountered on a customer's machine is caused by other issues. Maybe the underlying VM or hardware is broken.
Thanks for your help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With