Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

log4net, can 2 applications write to the same log file?

Is it possible to have 2 applications write to the same log file using log4net?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MinimalLock partially solves the problem (as @Mark mentioned), but if you're using RollingFileAppender, you'll run into other problems. When the file rolls, you may find yourself in a race condition where one process overwrites another process's newly created log file.

Other options include RemoteLogger, where you have a simple server set up to receive and record logging events sent by other processes. Likewise, you can log to a SQL database. I wrote a simple appended that logs to Redis; you'd need a simple application to read from Redis and record to a file. The problem with these approaches is they all introduce a point of failure. When something isn't working right is often when you need logs the most, and then they might not be available.

So my solution was to avoid the problem altogether by having each process log to its own file. This was easy to do with a change in configuration. In your (Rolling)FileAppender configuration, use:

<file type="log4net.Util.PatternString" value="c:mylog-[%processid].txt" />

The process ID becomes part of the file name. Yes, this means you now have several log files to comb through, but a log file aggregator like Graylog, Splunk, or Logscape can help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...