Component: NServiceBus
NuGet Package NServiceBus (7.x)


This sample shows how to use the notification API to capture the following:

Custom Settings in this sample

This sample uses several non-standard settings.


All errors below Fatal are suppressed to reduce the noise related to raising multiple exceptions

var defaultFactory = LogManager.Use<DefaultFactory>();

Delayed Retry Time increase

The time to increase changed to 1 second so the wait for all retries to occur is reduced.

var recoverability = endpointConfiguration.Recoverability();
    customizations: delayed =>

Plugging to the API

The notifications API is exposed as follows.

var endpointConfiguration = new EndpointConfiguration("Samples.Notifications");
public static class SubscribeToNotifications
    static ILog log = LogManager.GetLogger(typeof(SubscribeToNotifications));

    public static void Subscribe(EndpointConfiguration endpointConfiguration)
        var errors = endpointConfiguration.Notifications.Errors;
        errors.MessageHasBeenSentToDelayedRetries += (sender, retry) => Log(retry);
        errors.MessageHasFailedAnImmediateRetryAttempt += (sender, retry) => Log(retry);
        errors.MessageSentToErrorQueue += (sender, retry) => Log(retry);

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

    static void Log(FailedMessage failed)
        log.Fatal($@"Message sent to error queue.

    static void Log(DelayedRetryMessage retry)
        log.Fatal($@"Message sent to Delayed Retries.

    static void Log(ImmediateRetryMessage retry)
        log.Fatal($@"Message sent to Immedediate Retry.

Notifications are manipulated at configuration time.

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.

Message Body

Since the message could have failed due to a deserialization 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 are required for debugging purposes, convert the message body to a string using the .NET Encoding APIs.

Related Articles

  • Recoverability
    Explains how exceptions are handled, and actions retried, during message processing.

Last modified