Before even start trying any of the alternatives provided, ask yourself whether you really need to has multiple proce SSEs log to the same file and then don ' t do it;-).
Fileappender offers pluggable locking models for this usecase but all existing implementations has issues and drawbacks.
By default the Fileappender holds a exclusive write lock on the log file while it is logging. This prevents and processes from writing to the file. This model is known-to-break-with (at least-some versions of) Mono on Linux and log files may get corrupted as Soo N as another process tries to access the log file.
Minimallock only acquires the write lock and a log is being written. This allows multiple processes to Interleave writes to the same file, and albeit with a considerable loss in performance.
Interprocesslock doesn ' t lock the file at all but synchronizes using a system wide Mutex. This would only work if all processes cooperate (and use the same locking model). The acquisition and release of a Mutex for every log entry to be written would result in a loss of performance, but the Mut Ex is preferable to the use of minimallock.
If You use rollingfileappender things become even worse as several process could try to start rolling the log file Concu rrently. rollingfileappendercompletely ignores the locking model when rolling files, rolling files are simply not compatible wit H this scenario.
A better alternative is to has your processes log to remotingappenders. Using the remoteloggingserverplugin (or iremoteloggingsink) a process can receive all the events and logs them to a Single log file. One of the examples shows how to use the Remoteloggingserverplugin.
In this case, it is roughly a file-like write log that encounters multiple process writes to the same log file at the same time. Finally, the best way to do this is to use Remotingappender. Then took a moment to study it. Mainly want to say a few points of attention. Configuration of the Client
<appender name= "Remotingappender" type= "log4net. Appender.remotingappender "> <sink value=" Tcp://localhost:8085/loggingsink "/> <lossy value=" False "/> <buffersize value=" "/> <onlyfixpartialeventdata value=" true "/></appender >
Note: buffersize is a buffer size, Remotingappender not immediately send the log to the server, wait until the buffer is full of the size of the set before the hair, if you want to send immediately please set this value to 0
Of course Remotingappender and other appender can set the log format.
Mainly about the service side
First registers the channel, the client configures the TCP so the server side also uses TcpChannel registers the time ChannelServices.RegisterChannel (channel, false); Note If the second parameter is set to false;
(I've been spending a lot of time here).
Registering a remote object using Remotingservices.marshal (note log4net internally using an interface mode to activate the remote object)
Remote objects must inherit Remotingappender.iremoteloggingsink excuses
Log4net. Logmanager.getrepository (). Pluginmap.add (New log4net. Plugin.remoteloggingserverplugin ("Loggingsink"))
where "Loggingsink" is the name of the remote object publication
Using the second way, you do not need to implement the remote object yourself, log4net to help us achieve a remote object. To get log information, just configure a appender on the server side!
Configuration of the Log4net Remotingappender