Error notifications are available for several events:
- When an immediate retry occurs.
- When a delayed retry occurs.
- When a message fails all retries and is forwarded to the error queue.
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, ct) =>
{
log.Info($"Message {retry.MessageId} will be retried immediately.");
return Task.CompletedTask;
}));
recoverability.Delayed(settings => settings.OnMessageBeingRetried((retry, ct) =>
{
log.Info($@"Message {retry.MessageId} will be retried after a delay.");
return Task.CompletedTask;
}));
recoverability.Failed(settings => settings.OnMessageSentToErrorQueue((failed, ct) =>
{
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.
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.