As mentioned in the original post, the recommended solution is to use ServicePulse to monitor errors. The client I am currently working with is using a centralized, custom-made registrar, and we want NServiceBus to register in this log repository when messages are sent to the error queue.
We could achieve this by simply editing the log4net configuration, if the exception from NServiceBus included the original exception, currently NServiceBus just logs a generic error message without any details about what caused the failure.
NServiceBus has a class called NServiceBus.Faults.ErrorsNotifications that contains the following observable data:
- MessageSentToErrorQueue
- MessageHasFailedAFirstLevelRetryAttempt
- MessageHasBeenSentToSecondLevelRetries
, , , , :
public class GlobalErrorHandler : IWantToRunWhenBusStartsAndStops
{
private readonly ILogger _logger;
private readonly BusNotifications _busNotifications;
readonly List<IDisposable> _notificationSubscriptions = new List<IDisposable>();
public GlobalErrorHandler(ILogger logger, BusNotifications busNotifications)
{
_logger = logger;
_busNotifications = busNotifications;
}
public void Start()
{
_notificationSubscriptions.Add(_busNotifications.Errors.MessageSentToErrorQueue.Subscribe(LogWhenMessageSentToErrorQueue));
}
public void Stop()
{
foreach (var subscription in _notificationSubscriptions)
{
subscription.Dispose();
}
}
private void LogWhenMessageSentToErrorQueue(FailedMessage message)
{
var properties = new
{
MessageType = message.Headers["NServiceBus.EnclosedMessageTypes"],
MessageId = message.Headers["NServiceBus.MessageId"],
OriginatingMachine = message.Headers["NServiceBus.OriginatingMachine"],
OriginatingEndpoint = message.Headers["NServiceBus.OriginatingEndpoint"],
ExceptionType = message.Headers["NServiceBus.ExceptionInfo.ExceptionType"],
ExceptionMessage = message.Headers["NServiceBus.ExceptionInfo.Message"],
ExceptionSource = message.Headers["NServiceBus.ExceptionInfo.Source"],
TimeSent = message.Headers["NServiceBus.TimeSent"]
};
_logger.Error("Message sent to error queue. " + properties, message.Exception);
}
}
Reactive Extensions, Rx-Core NuGet .