NServiceBus 是否有全局异常处理程序? [英] Is there a global exception handler for NServiceBus?

查看:59
本文介绍了NServiceBus 是否有全局异常处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前关于在 NServiceBus 中处理异常的建议 是使用内置工具.错误消息进入错误消息队列,并将日志写入磁盘.

The current advice on handling exceptions in NServiceBus is to use the built in facilities. Errored out messages go to the error message queue, and a log is written to disk.

但是,如果我想将错误发送到 AirBrake 这样的服务,该服务具有更好的功能来对类似的异常、指标和其他好东西进行分组,该怎么办?是否有我可以使用的全局异常处理程序?

But what if I want to send my errors to a service like AirBrake which has better functionality for grouping similar exceptions, metrics, and other good stuff? Is there a global exception handler I can tap into?

推荐答案

如原帖所述,推荐的解决方案是使用 ServicePulse 来监控错误.我目前工作的客户端使用定制的集中式记录器,我们希望 NServiceBus 在消息转发到错误队列时记录到此日志存储.

As mentioned in the original post the recommended solution is to use ServicePulse for monitoring errors. The client I currently work for is using a custom made centralized logger, and we want NServiceBus to log to this log store when messages are forwarded to the error queue.

如果来自 NServiceBus 的异常包含原始异常,我们本可以通过编辑 log4net 配置来实现这一点,目前 NServiceBus 只记录一条通用错误消息,没有详细说明导致失败的原因.

We could have achieved this by just editing the log4net config if the exception from NServiceBus had included the original exception, currently NServiceBus just logs a generic error message with no details about what caused the failure.

NServiceBus 有一个名为 NServiceBus.Faults.ErrorsNotifications 的类,它包含以下可观察对象:

NServiceBus has a class named NServiceBus.Faults.ErrorsNotifications which contains the following observables:

  • MessageSentToErrorQueue
  • MessageHasFailedAFirstLevelRetryAttempt
  • MessageHasBeenSentToSecondLevelRetries

您可以在端点启动时订阅这些 observable,如下例所示,当消息发送到错误队列时会记录错误:

You can subscribe to these observables when the endpoint starts, like in the following example which logs an error when messages are sent to the error queue:

public class GlobalErrorHandler : IWantToRunWhenBusStartsAndStops
{
    private readonly ILogger _logger;
    private readonly BusNotifications _busNotifications;
    readonly List<IDisposable> _notificationSubscriptions = new List<IDisposable>();

    public GlobalErrorHandler(ILogger logger, BusNotifications busNotifications)
    {
        _logger = logger;
        _busNotifications = busNotifications;
    }

    public void Start()
    {
        _notificationSubscriptions.Add(_busNotifications.Errors.MessageSentToErrorQueue.Subscribe(LogWhenMessageSentToErrorQueue));
    }

    public void Stop()
    {
        foreach (var subscription in _notificationSubscriptions)
        {
            subscription.Dispose();
        }
    }

    private void LogWhenMessageSentToErrorQueue(FailedMessage message)
    {
        var properties = new
        {
            MessageType = message.Headers["NServiceBus.EnclosedMessageTypes"],
            MessageId = message.Headers["NServiceBus.MessageId"],
            OriginatingMachine = message.Headers["NServiceBus.OriginatingMachine"],
            OriginatingEndpoint = message.Headers["NServiceBus.OriginatingEndpoint"],
            ExceptionType = message.Headers["NServiceBus.ExceptionInfo.ExceptionType"],
            ExceptionMessage = message.Headers["NServiceBus.ExceptionInfo.Message"],
            ExceptionSource = message.Headers["NServiceBus.ExceptionInfo.Source"],
            TimeSent = message.Headers["NServiceBus.TimeSent"]
        };

        _logger.Error("Message sent to error queue. " + properties, message.Exception);
    }
}

observable 是通过使用 Reactive Extensions 实现的,因此您必须安装 NuGet 包 Rx-Core 才能使其工作.

The observable is implemented by using Reactive Extensions, so you will have to install the NuGet package Rx-Core for this to work.

这篇关于NServiceBus 是否有全局异常处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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