Như đã đề cập trong các bài bản gốc các giải pháp được đề nghị là sử dụng ServicePulse để theo dõi lỗi. Ứng dụng khách hiện tôi đang làm là sử dụng trình ghi tập trung tùy chỉnh được thực hiện và chúng tôi muốn NServiceBus đăng nhập vào cửa sổ nhật ký này khi các thư được chuyển tiếp đến hàng đợi lỗi. Chúng tôi có thể đạt được điều này bằng cách chỉnh sửa cấu hình log4net nếu ngoại lệ từ NServiceBus đã bao gồm ngoại lệ ban đầu, hiện tại NServiceBus chỉ ghi nhật ký thông báo lỗi chung mà không có chi tiết về nguyên nhân gây ra lỗi.
NServiceBus có một lớp có tên NServiceBus.Faults.ErrorsNotifications chứa các quan sát sau:
- MessageSentToErrorQueue
- MessageHasFailedAFirstLevelRetryAttempt
- MessageHasBeenSentToSecondLevelRetries
Bạn có thể đăng ký vào những quan sát khi thiết bị đầu cuối bắt đầu, như trong ví dụ sau đây ghi nhật ký lỗi khi thư nt vào hàng đợi lỗi:
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);
}
}
Quan sát được thực hiện bằng cách sử dụng Tiện ích mở rộng phản ứng, vì vậy bạn sẽ phải cài đặt gói RG-Core NuGet để hoạt động.
Điều đó thật sự rất tệ nếu bạn không thể sử dụng nó với lần thử lại ở cấp độ hai. Cảm ơn, mặc dù! –