Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active November 20, 2025 12:54
Show Gist options
  • Select an option

  • Save ramonsmits/cc3a8b91e1d496a10e512cdb048d6c2b to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/cc3a8b91e1d496a10e512cdb048d6c2b to your computer and use it in GitHub Desktop.
NServiceBus behavior that logs incoming message details
//
// Register as:
//
// endpointConfiguration.Pipeline.Register(new LogIncomingMessageDetails(), nameof(LogIncomingMessageDetails));
//
sealed class LogIncomingMessageDetails : Behavior<IIncomingPhysicalMessageContext>
{
static readonly ILog logger = LogManager.GetLogger<LogIncomingMessageDetails>();
public override async Task Invoke(IIncomingPhysicalMessageContext context, Func<Task> next)
{
var nativeId = context.Message.NativeMessageId;
var id = context.MessageId;
var enclosedMessageTypes = context.MessageHeaders[Headers.EnclosedMessageTypes];
try
{
logger.DebugFormat("Processing {0}, {1}, {2}", nativeId, id, enclosedMessageTypes);
await next();
logger.DebugFormat("Completed {0}, {1}, {2}", nativeId, id, enclosedMessageTypes);
}
catch
{
logger.ErrorFormat("Failed {0}, {1}, {2}", nativeId, id, enclosedMessageTypes);
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment