Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net write file from many processes

Is it possible to write from 5 different processes to the same log file?

I am using Log4Net for logging, but seems like only 1 process is writing to the file, when I shut this process down, the 2nd process is writing.

I want all to write together.

How to?

like image 807
m0fo Avatar asked Oct 06 '12 20:10

m0fo


2 Answers

If you want to write to a single file from multiple processes add the MinimalLock as LockinModel to your <appender> node:

<appender .... >
  ......
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  ......
</appender>

Beware this have some impact on the performance.

like image 108
Erwin Avatar answered Sep 30 '22 17:09

Erwin


Though @Erwin's advice works in most cases, you should reconsider if a better architecture can help improve performance.

For example, Microsoft IIS server has many worker processes running, each sending log entries to IIS service process via a named pipe. And only IIS service process has the right to write to log files. In this way, worker processes do not need to lock log files, and the service process can cache entries and write them in batches.

It is very easy to follow IIS's approach and achieve good performance. without file locks.

(Updated: You can now use LogMaster4Net, which implements such an architecture based on UDP.)

like image 37
Lex Li Avatar answered Sep 30 '22 16:09

Lex Li