Is there a global exception handler for NServiceBus?

The current exception handling tip in NServiceBus is to use built-in tools. Corrected messages go into the error message queue, and the log is written to disk.

But what if I want to send my errors to AirBrake, which has the best functionality for grouping such exceptions, metrics, and other good things? Is there a global exception handler that I can use?

+5
source share
5 answers

As mentioned in the original post, the recommended solution is to use ServicePulse to monitor errors. The client I am currently working with is using a centralized, custom-made registrar, and we want NServiceBus to register in this log repository when messages are sent to the error queue.

We could achieve this by simply editing the log4net configuration, if the exception from NServiceBus included the original exception, currently NServiceBus just logs a generic error message without any details about what caused the failure.

NServiceBus has a class called NServiceBus.Faults.ErrorsNotifications that contains the following observable data:

  • MessageSentToErrorQueue
  • MessageHasFailedAFirstLevelRetryAttempt
  • MessageHasBeenSentToSecondLevelRetries

, , , , :

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);
    }
}

Reactive Extensions, Rx-Core NuGet .

+4

NServiceBus , IManageMessageFailures, , , , , , .

SQL Server log4net, AirBrake, API, ​​ , , ?

+1

, AirBrake.

: v4.0 | 4.1 Rest-Api, / . , Profiler Ops, api .

http://particular.net/service-platform

+1

We switched to Serilog, it has excellent support for tracking and tracking NServiceBus. Creating a custom Serilog receiver that will send your log events wherever you are is quite simple.

+1
source

All Articles