m0fo
m0fo

Reputation: 2189

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?

Upvotes: 13

Views: 8589

Answers (2)

Lex Li
Lex Li

Reputation: 63183

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.)

Upvotes: 5

Erwin
Erwin

Reputation: 4817

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.

Upvotes: 19

Related Questions