log4net 停止记录时如何接收事件

发布于 2024-07-29 20:46:11 字数 223 浏览 4 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

最近可好 2024-08-05 20:46:11

可能对您有用的方法是创建一个实现 IErrorHandler 的类 对于每个附加程序,然后配置每个附加程序以使用自定义错误处理类。 与启用 log4net.Internal.Debug 相比,这应该为您提供更多的控制权。

我刚刚尝试过,它有效(请注意,在我的示例中 Logger 是在其他地方定义的 log4net 记录器 - 其背后的目的是从 SMTP 附加程序捕获错误并将其记录到文件中):

using System;

using log4net.Core;

namespace Test
{
    public class SmtpErrorHandler : IErrorHandler
    {
        public void Error(string message)
        {
            Logger.Log.Error(message);
        }

        public void Error(string message, Exception ex)
        {
            Logger.Log.Error(message, ex);
        }

        public void Error(string message, Exception ex, ErrorCode errorCode)
        {
            Logger.Log.Error(String.Format("{0}{1}Error code: {2}", message, Environment.NewLine, errorCode), ex);
        }
    }
}

我在哪里配置我的附加程序(当然你也可以在配置中执行此操作):

emailAppender.ErrorHandler = new SmtpErrorHandler();

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):

using System;

using log4net.Core;

namespace Test
{
    public class SmtpErrorHandler : IErrorHandler
    {
        public void Error(string message)
        {
            Logger.Log.Error(message);
        }

        public void Error(string message, Exception ex)
        {
            Logger.Log.Error(message, ex);
        }

        public void Error(string message, Exception ex, ErrorCode errorCode)
        {
            Logger.Log.Error(String.Format("{0}{1}Error code: {2}", message, Environment.NewLine, errorCode), ex);
        }
    }
}

Where I configure my appender (of course you can do this in the config too):

emailAppender.ErrorHandler = new SmtpErrorHandler();
梦晓ヶ微光ヅ倾城 2024-08-05 20:46:11

好吧,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.

拥有 2024-08-05 20:46:11

我们创建了自己的自定义类,它执行 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.

征棹 2024-08-05 20:46:11

如果您使用诸如 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.

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