Notifications

Component: NServiceBus
NuGet Package NServiceBus (6.x)

Introduction

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.

Logging

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

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

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();
recoverability.Delayed(
    customizations: delayed =>
    {
        delayed.TimeIncrease(TimeSpan.FromSeconds(1));
    });

Plugging to the API

The notifications API is exposed as follows.

var endpointConfiguration = new EndpointConfiguration("Samples.Notifications");
SubscribeToNotifications.Subscribe(endpointConfiguration);
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.
        Body:
        {GetMessageString(failed.Body)}");
    }

    static void Log(DelayedRetryMessage retry)
    {
        log.Fatal($@"Message sent to Delayed Retries.
        RetryAttempt:{retry.RetryAttempt}
        Body:
        {GetMessageString(retry.Body)}");
    }

    static void Log(ImmediateRetryMessage retry)
    {
        log.Fatal($@"Message sent to Immedediate Retry.
        RetryAttempt:{retry.RetryAttempt}
        Body:
        {GetMessageString(retry.Body)}");
    }
}

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 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.

Related Articles

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

Last modified