Log4net在此模式下不允许递归读锁定采集

时间:2015-08-21 02:58:30

标签: c# .net log4net

我有一个自定义的log4net appender,如下所示:

public class MyAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
        try
        {
            if (loggingEvent.Level == Level.Error || loggingEvent.Level == Level.Fatal)
            {
                DoWork(RenderLoggingEvent(loggingEvent));
            }
        }
        catch
        {
            // silently fail 
        }
    }
}

我时不时地在输出中看到这个例外:

log4net:ERROR Exception while logging
System.Threading.LockRecursionException: Recursive read lock acquisitions not allowed in this mode.
   at System.Threading.ReaderWriterLockSlim.TryEnterReadLockCore(TimeoutTracker timeout)
   at System.Threading.ReaderWriterLockSlim.TryEnterReadLock(TimeoutTracker timeout)
   at System.Threading.ReaderWriterLockSlim.EnterReadLock()
   at log4net.Util.ReaderWriterLock.AcquireReaderLock()
   at log4net.Repository.Hierarchy.Logger.CallAppenders(LoggingEvent loggingEvent)
   at log4net.Repository.Hierarchy.Logger.ForcedLog(Type callerStackBoundaryDeclaringType, Level level, Object message, Exception exception)
   at log4net.Repository.Hierarchy.Logger.Log(Type callerStackBoundaryDeclaringType, Level level, Object message, Exception exception)

appender似乎工作正常,我的应用程序没有显示任何错误,但这些消息太烦人了。我喜欢干净的运行软件,这不干净=(

我的appender实现是否正常?我可以包含一些配置以避免此错误吗?我可以从gooogs找到的最接近的答案来自这个错误报告log4net bug。我没有调用GetAppenders,因此它不适用于我的用例。任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:4)

在我的情况下,这发生在我的Appender调用正在记录的代码时。因此,发生了递归记录。这可以在下面看到:

-> Appender -> MyClassThatLogs -> Appender -> MyClassThatLogs -> etc...

另见SO帖子: logging/error handling inside a custom log4net appender