如何配置 log4net 进行回退

发布于 2024-09-09 20:09:07 字数 194 浏览 3 评论 0原文

这就是我的情况。我已经使用 log4net 成功实现了远程系统日志记录。但是,据我测试,如果 syslog IP 无效,所有消息都不会记录在任何地方,并且不会引发异常。它只是什么都不做。

因此,最好有某种后备措施。假设如果写入系统日志失败,则写入文件或数据库。

log4net 可以做到这一点吗?或者我是否必须将其配置为同时登录到两个位置?

This is my situation. I have successfully implemented logging to remote syslog using log4net. However, as far as I could test, if syslog IP is not valid, all messages will not log anywhere and no exception is raised. It just does nothing.

Hence, it would be nice to have some sort of fallback. Let's say if writing to syslog fails, write to file or to database.

Is that even possible with log4net? Or would I have to configure it to log to two locations at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

假装不在乎 2024-09-16 20:09:07

我认为你不能通过配置来做到这一点。此问题在 log4net 功能待办事项中开放

如果您的应用程序可以消耗日志记录开销,那么最简单的解决方案是默认记录到备用附加程序。

或者,您可以尝试将您正在使用的附加程序包装在自定义附加程序中,并在系统日志附加程序抛出异常时实现回退方案。如果它不默默地吞噬它们的话。

来自 log4net 常见问题解答

您可以实现 log4net.Appender.IAppender 接口来创建您自己的自定义附加程序。我们建议您扩展 log4net.Appender.AppenderSkeleton 类,而不是从头开始。您应该在与 log4net 程序集分开的程序集中实现自定义代码。

首先,值得查看 log4net.Appender.TraceAppender 的源代码,作为使附加程序工作所需的最少代码量的示例。

第三种选择是查看附加程序的源代码,看看是否可以分叉它并在那里进行必要的自定义。

I don't think you can do this by configuration. This issue is open in the log4net feature backlog.

If your application can eat the logging overhead, the easiest solution would be to log to an alternative appender by default.

Alternatively you could try to wrap the appender you're using in a custom appender, and implement the fallback scenario if the syslog appender throws an exception. If it doesn't swallow them silently.

From log4net FAQ:

You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly.

To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.

Third option would be to look into the source code of your appender and see if you can fork it and do the necessary customizations there.

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