Error notifications

Component: NServiceBus
NuGet Package NServiceBus (7.2)

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.

var recoverability = endpointConfiguration.Recoverability();

recoverability.Immediate(settings => settings.OnMessageBeingRetried(retry =>
{
    log.Info($"Message {retry.MessageId} will be retried immediately.");
    return Task.CompletedTask;
}));

recoverability.Delayed(settings => settings.OnMessageBeingRetried(retry =>
{
    log.Info($@"Message {retry.MessageId} will be retried after a delay.");
    return Task.CompletedTask;
}));

recoverability.Failed(settings => settings.OnMessageSentToErrorQueue(failed =>
{
    log.Fatal($@"Message {failed.MessageId} will be sent to the error queue.");
    return Task.CompletedTask;
}));
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.

Unsubscribing

Since notifications are global for the current endpoint, it is also important to ensure that subscriptions that are no longer required are removed so as to not unnecessarily impact performance. This is done by detaching from the event.

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.

Samples


Last modified