服务器代码
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")
客户端使用远程附加程序到服务器
服务器的 RemoteSink::LogEvents 在工作线程中运行,服务器在主线程中记录一些内容,我发现有时它卡住了,我连接了,VC 显示它卡在 log4net.dll!log4net.Appender.AppenderSkeleton.DoAppend(log4net.Core.LoggingEvent loggingEvent = {log4net.Core.LoggingEvent}) + 0x52 字节,可能源行“lock(this)”在哪里
我搜索互联网,每个人都说log4net
是线程安全的,我错在哪里?
================================解决================== ========================== 我被调用堆栈误导了,这让我想到了log4net
错误
我还在我的服务器中使用了一个RichTextBoxAppender
(使用 log4net 的 RichTextBoxAppender )
使用,它应该RichTextBoxAppender
用来使线程安全Invoke
BeginInvoke