log4net 停止记录时如何接收事件
Log4net 有点太擅长不抛出错误了。 我正在尝试创建某种处理程序,如果 log4net 无法启动或死亡并且无法再记录,则会触发该处理程序。
我知道应用程序设置键可以打开 log4net 的内部调试 (log4net.Internal.Debug)。 不过,只要 log4net 出现问题,我并不总是需要所有调试信息。
有没有人有办法以编程方式捕获和处理 log4net 中的错误?
Log4net is a little too good at not throwing errors. I am trying to create some kind of handler that fires if log4net can not start or dies and can no longer log.
I am aware of the app settings key to turn on log4net's internal debugging (log4net.Internal.Debug). I don't need all the debugging information all the time though, just if there is an issue with log4net.
Does anyone have a way they have programmatically captured and handled errors in log4net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能对您有用的方法是创建一个实现 IErrorHandler 的类 对于每个附加程序,然后配置每个附加程序以使用自定义错误处理类。 与启用 log4net.Internal.Debug 相比,这应该为您提供更多的控制权。
我刚刚尝试过,它有效(请注意,在我的示例中
Logger
是在其他地方定义的 log4net 记录器 - 其背后的目的是从 SMTP 附加程序捕获错误并将其记录到文件中):我在哪里配置我的附加程序(当然你也可以在配置中执行此操作):
Something that may work for you is to create a class that implements IErrorHandler for each appender, then configure each appender to use the custom error handling class. This should give you more control than enabling log4net.Internal.Debug.
I've just given this a try and it works (note that in my sample
Logger
is a log4net logger defined elsewhere - the aim behind this is to capture errors from an SMTP appender and log them to a file):Where I configure my appender (of course you can do this in the config too):
好吧,log4net 会让你很难做到这一点,因为它(根据设计)会抑制日志记录操作期间引发的异常。 这是因为生产系统不应该因为格式化日志消息时出现错误而失败。
值得尝试非常简单的图案布局,因为有时在图案布局中使用 %P{XYZ} 元素,如果相应的属性设置不正确,就会导致问题。 如果通过简单的图案布局一切都按预期工作,您可以一次将所需的内容添加回其中,看看是否可以通过这种方式查明问题。
Well, log4net will make it very hard for you to do this, since it will (by design) suppress exceptions which are thrown during logging operations. That's because a production system shouldn't fail because of an error while formatting a log message.
It's worth trying with very simple pattern layouts, as sometimes it's the use of %P{XYZ} elements in pattern layouts which cause problems if the corresponding properties are not properly set. If everything works as expected with a simple pattern layout, you can add the things you need back in one at a time and see if you can pinpoint the problem that way.
我们创建了自己的自定义类,它执行 Logger.Log.Error...,如果抛出异常或出现问题,我们会将原始错误和异常写入 Windows 事件日志。
在分析错误时,用户不仅要向我们发送日志文件,还要向我们发送事件日志。 我们创建了一个工具,可以自动导出事件日志并将其与所有可用的 Log4net 日志文件一起压缩到 zip 文件中。
We created our own custom class which performs Logger.Log.Error... and if a exception is thrown or something went wrong, we write the original error and also the exception into the Windows Event Log.
When analyzing the errors, the user has to send us not only the logfiles, but also the event log. We created a tool which automatically exports the event logs and compress them together with all available Log4net logfiles into a zip file.
如果您使用诸如 SCOM(System Center Operations Manager)之类的监视工具,您可以将其设置为监视日志文件,并在超过 n 小时/天没有进行任何更改时向您发出警报。
If you use a monitoring tool such as SCOM (System Center Operations Manager) you can set it to monitor the log files and alert you when no changes have been made for more than n hours / days.