Resolve deadlock log4net

server code

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)); } 

and the server has 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); 

therefore the server can also log.Info("xxxxx")

the client uses a remote appender to the server

The RemoteSink :: LogEvents server is running in a workflow, and the server is registering something in mainthread, and sometimes I find that it hangs, I connect, and VC shows that it is stuck on log4net.dll! log4net.Appender.AppenderSkeleton.DoAppend (log4net.Core.LoggingEvent loggingEvent = {log4net.Core.LoggingEvent}) + 0x52 bytes, possibly where the original lock (this) line is

I search on the Internet, everyone says that log4net is safe, where am I wrong?

================================ solved ================== =========================== I am misleading by the call stack, which makes me think about the log4net error

I also used RichTextBoxAppender ( RichTextBoxAppender using log4net ) on my server

RichTextBoxAppender uses Invoke , it must use BeginInvoke to make it thread safe

+5
source share

Source: https://habr.com/ru/post/1502382/