开发者

log4net: Specifying different layouts for different kinds of messages in the same RollingFileAppender

Apparently two RollingFileAppenders cannot write to the same file (or has it changed since?).

So how can I write info messages with one layout and exceptions with another layout, both to the same rolling tile? Is it possib开发者_运维问答le to specify two different layouts in the same appender, or is there any other solution?


Try adding following element into FileAppender/RollingFileAppender elements of each XML config file:

<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />

I'm using this to log from many log4net powered executables to a single file.


It is not really an issue of the (File)Appender, but much more so of the LockingModel behind it.

As "Random" has said, you might use the MinimalLock, note however, that it has two main disadvantages:

  • Rather bad performance compared to ExclusiveLock, because the file lock is acquired/released for every message that is written.
  • It just prevents two processes from writing concurrently. It may still happen that more than one process attempts to write to a single file. In this case only one will actually be granted, all others get errors and their messages are lost (also see here).

To circumvent this, you can write your own LockingModel which internally uses a Mutex. This is (a) faster then MinimalLock's filelocking and (b) still allows all processes to eventually write their message (even thouhg, as intendend of course, mutally exclusive). I have an implementation that I unfortunately cannot share, because it is from a customer's project. But it shouldn't be too hard to come up with an implementation (their might be one on the web, but I cannot find it just yet).

Edit: There is an implementation in this mailing list thread. Personally, I have not used it, so I cannot tell if it works, but it looks OK.

Update: Just happen to notice the "different layouts" part of the orginal question. I guess it is not possible to have a single appender using two different layouts. You could have two appenders which are equally configured, except for the layouts. Then have them write to the same file using a custom lockingModel or MinimalLock - YMMV. However, I really wouldn't do that. Having two layouts (formats!) in the same logfile makes parsing or log analysis unnecessary hard. Why not have two files in the first place then?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜