log4net:在同一个RollingFileAppender中为不同类型的消息指定不同的布局
显然两个 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 possible to specify two different layouts in the same appender, or is there any other solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将以下元素添加到 FileAppender/RollingFileAppender 元素中
每个 XML 配置文件:
我使用它从许多 log4net 支持的可执行文件记录到单个文件。
Try adding following element into FileAppender/RollingFileAppender elements of
each XML config file:
I'm using this to log from many log4net powered executables to a single file.
这实际上并不是 (File)Appender 的问题,而是其背后的 LockingModel 的问题。
正如“Random”所说,您可能会使用 MinimalLock,但请注意,它有两个主要缺点:
为了避免这种情况,您可以编写自己的 LockingModel 其内部使用互斥体。这(a)比 MinimalLock 的文件锁定更快,并且(b)仍然允许所有进程最终写入它们的消息(尽管,当然,作为预期目标,是互斥的)。不幸的是,我有一个实现无法分享,因为它来自客户的项目。但想出一个实现应该不会太难(他们可能是在网上的,但我还找不到它)。
编辑:此邮件列表线程。就我个人而言,我没有使用过它,所以我无法判断它是否有效,但看起来还不错。
更新:只是碰巧注意到原始问题的“不同布局”部分。我想不可能有一个附加程序使用两种不同的布局。您可以有两个配置相同的附加程序(布局除外)。然后让他们使用自定义锁定模型或 MinimalLock - YMMV 写入同一文件。不过,我真的不会那么做。在同一个日志文件中拥有两种布局(格式!)会使解析或日志分析变得不必要的困难。那么为什么不首先有两个文件呢?
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:
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?