log4net:在同一个RollingFileAppender中为不同类型的消息指定不同的布局

发布于 2024-10-27 08:01:37 字数 220 浏览 3 评论 0原文

显然两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2024-11-03 08:01:37

尝试将以下元素添加到 FileAppender/RollingFileAppender 元素中
每个 XML 配置文件:

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

我使用它从许多 log4net 支持的可执行文件记录到单个文件。

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.

半世晨晓 2024-11-03 08:01:37

这实际上并不是 (File)Appender 的问题,而是其背后的 LockingModel 的问题。

正如“Random”所说,您可能会使用 MinimalLock,但请注意,它有两个主要缺点:

  • 与 ExclusiveLock 相比,性能相当差,因为每条写入的消息都会获取/释放文件锁。
  • 它只是防止两个进程同时写入。仍然可能会发生多个进程尝试写入单个文件的情况。在这种情况下,实际上只有一个被授予,所有其他人都会出错并且他们的消息都会丢失(另请参阅此处)。

为了避免这种情况,您可以编写自己的 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:

  • 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文