Created
July 16, 2019 08:44
-
-
Save ramonsmits/e263f6d38db13556f19604ae09649bf0 to your computer and use it in GitHub Desktop.
NServiceBus - Log all incoming messages handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NServiceBus; | |
using NServiceBus.Logging; | |
class LogHandler : IHandleMessages<object> | |
{ | |
static readonly ILog Log = LogManager.GetLogger<LogHandler>(); | |
public IBus Bus { get; set; } | |
public void Handle(object message) | |
{ | |
var id = Bus.CurrentMessageContext.Id; | |
var headers = Bus.CurrentMessageContext.Headers; | |
var sent = DateTimeExtensions.ToUtcDateTime(headers[Headers.TimeSent]); | |
var now = DateTime.UtcNow; | |
var duration = DateTime.UtcNow - sent; | |
Log.Debug($"Received {id} at {now}, sent {sent} ({duration.TotalMilliseconds:N0}ms) - {message.GetType().Name} {message}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment