如何配置log4net以进行回退 [英] How to configure log4net for fallback

查看:72
本文介绍了如何配置log4net以进行回退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

因此,最好有某种后备.假设写入syslog失败,则写入文件或数据库.

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.

使用log4net甚至可以吗?还是我必须将其配置为同时登录到两个位置?

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

推荐答案

我认为您无法通过配置来做到这一点.此问题是在log4net功能积压中打开的.

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.

或者,您可以尝试将正在使用的附加程序包装在自定义附加程序中,如果syslog附加程序引发异常,则可以实现后备方案.如果它不能默默地吞下他们.

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.

来自 log4net常见问题解答:

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

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.

首先,值得一看的是log4net.Appender.TraceAppender的源代码,作为使追加程序正常工作所需的最少代码量的示例.

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.

这篇关于如何配置log4net以进行回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆