解决log4net死锁问题

时间:2013-09-16 10:24:28

标签: c# multithreading log4net

服务器代码

public class RemoteSink : MarshalByRefObject, RemotingAppender.IRemoteLoggingSink
{
    public void LogEvents(LoggingEvent[] events)
    {
        foreach (var loggingEvent in events)
        {
            LoggingEventData logData = loggingEvent.GetLoggingEventData();
            logData.Message = "[" + logData.Domain + "] " + logData.Message;
            log4net.LogManager.GetRepository().Log(new LoggingEvent(logData));
        }
    }
}

private static void SetRemoteLoggingServer()
{
    TcpChannel channel = new TcpChannel(15642);
    ChannelServices.RegisterChannel(channel, false);
    RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "LoggingSinkInConsoleDaemon", WellKnownObjectMode.SingleCall));
}

,服务器有RollingFileAppender

RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.DatePattern = @"yyyy-MM-dd.lo\g.\tx\t";
rollingFileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
rollingFileAppender.AppendToFile = true;
rollingFileAppender.File = logPath;
rollingFileAppender.Threshold = Level.All;
rollingFileAppender.StaticLogFileName = false;
rollingFileAppender.Layout = new PatternLayout(@"%date [t:%thread] %-5level ConsoleDaemon - %message%newline");
rollingFileAppender.MaxSizeRollBackups = 5;
rollingFileAppender.LockingModel = new FileAppender.MinimalLock();
rollingFileAppender.ActivateOptions();

BasicConfigurator.Configure(rollingFileAppender);

所以服务器也可以log.Info("xxxxx")

客户端使用远程appender到服务器

  

服务器的RemoteSink :: LogEvents在工作线程和服务器日志中运行   在mainthread中的东西,我发现有时它会卡住,我依旧和   VC显示它停留在   log4net.dll!log4net.Appender.AppenderSkeleton.DoAppend(log4net.Core.LoggingEvent   loggingEvent = {log4net.Core.LoggingEvent})+ 0x52字节,也许在哪里   是源代码行“lock(this)”

我搜索互联网,大家都说log4net是线程安全的,我错了吗?

===============================解决=============== ============================= 我误用了调用堆栈,这让我想到了log4net的错误

我还在我的服务器中使用了RichTextBoxAppenderRichTextBoxAppender using log4net

RichTextBoxAppender使用Invoke,它应该使用BeginInvoke来制作线程安全的

0 个答案:

没有答案