Error notifications

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

Error notifications are available for several events.

The following example shows how to be notified every time a message is handled by recoverability. While this code writes to the console any other action could be taken, for example sending an email or writing to a monitoring system.

Edit
public class SubscribeToNotifications :
    IWantToRunWhenBusStartsAndStops,
    IDisposable
{
    static ILog log = LogManager.GetLogger<SubscribeToNotifications>();
    BusNotifications busNotifications;
    List<IDisposable> unsubscribeStreams = new List<IDisposable>();

    public SubscribeToNotifications(BusNotifications busNotifications)
    {
        this.busNotifications = busNotifications;
    }

    public void Start()
    {
        var errorsNotifications = busNotifications.Errors;
        var defaultScheduler = Scheduler.Default;
        unsubscribeStreams.Add(
            errorsNotifications.MessageSentToErrorQueue
                .ObserveOn(defaultScheduler)
                .Subscribe(LogEvent)
            );
        unsubscribeStreams.Add(
            errorsNotifications.MessageHasBeenSentToSecondLevelRetries
                .ObserveOn(defaultScheduler)
                .Subscribe(LogEvent)
            );
        unsubscribeStreams.Add(
            errorsNotifications.MessageHasFailedAFirstLevelRetryAttempt
                .ObserveOn(defaultScheduler)
                .Subscribe(LogEvent)
            );
    }

    static string GetMessageString(byte[] body)
    {
        return Encoding.UTF8.GetString(body);
    }

    void LogEvent(FailedMessage failed)
    {
        log.Info($@"Message sent to error queue.
    Body:
    {GetMessageString(failed.Body)}");
    }

    void LogEvent(SecondLevelRetry retry)
    {
        log.Info($@"Message sent to Delayed Retries.
    RetryAttempt: {retry.RetryAttempt}
    Body:
    {GetMessageString(retry.Body)}");
    }

    void LogEvent(FirstLevelRetry retry)
    {
        log.Info($@"Message sent to Immediate Reties.
    RetryAttempt:{retry.RetryAttempt}
    Body:
    {GetMessageString(retry.Body)}");
    }

    public void Stop()
    {
        foreach (var unsubscribeStream in unsubscribeStreams)
        {
            unsubscribeStream.Dispose();
        }
    }

    public void Dispose()
    {
        Stop();
    }
}

Performance Impact

Notifications are executed on the same thread as the NServiceBus pipeline. If long running work needs to be done it should be executed on another thread otherwise the message processing performance can be impacted.

The notification instance is also injected into the container.

Reactive Extensions

The subscription is done via Reactive Extensions.

Unsubscribing

Since notifications are global for the current endpoint it is also important to ensure no longer required subscriptions removed so as to not unnecessarily impact performance.

This is done by keeping a reference to the IDisposable object returned from calling the Subscribe method on the IObservable and calling its Dispose method.

Message Body

Since the message could have failed due to a de-serialization exception it is not possible for the API to provide the instance of the message. For the same reason is it recommended that consumers of this API do not attempt to deserialize the message instance. If the message contents is required for debugging purposes it is recommended to convert it to a string using the .net encoding APIs.

Samples


Last modified