Thread Safety of log4net

不羁岁月 提交于 2019-12-20 10:08:14

问题


There seems to be some discussion on whether log4net is thread-safe, the consensus is that the framework is thread-safe, but appenders are not and need to be used correctly to achieve thread-safety. can someone shine some light on this and possibly give example of lets say RollingFileAppender used in a thread-safe way? does it need to be pushed into context? somehow locked, or what?


回答1:


According to this link, RollingFileAppender is thread safe (as far as logging is concerned). This is coming from one of the developers of log4net. He specifically says that locking like this is not required in your code:

lock(logger)
{
  logger.Info("Hello!");
}



回答2:


Straight from the log4net FAQ:

Is log4net thread-safe?

Yes, log4net is thread-safe.

So, no need for manual locking.




回答3:


But I've had a deadlock with ColoredConsoleAppender. And it sources comment states that internal writer (m_consoleOutputWriter) is not thread safe!

/// <summary>
/// The console output stream writer to write to
/// </summary>
/// <remarks>
/// <para>
/// This writer is not thread safe.
/// </para>
/// </remarks>
private System.IO.StreamWriter m_consoleOutputWriter = null;

And I've switched back to basic ConsoleAppender and have no issues since then.



来源:https://stackoverflow.com/questions/4098409/thread-safety-of-log4net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!