Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NServiceBus NLog - Use incoming message id for the Nested Diagnostics Logical Context (NDLC)
class AssignMessageIdtoNLogNdlcBehavior : Behavior<ITransportReceiveContext>
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
var headers = context.Message.Headers;
string conversationId, correlationId;
headers.TryGet(Headers.ConversationId, out conversationId);
headers.TryGet(Headers.CorrelationId, out correlationId);
using (NLog.NestedDiagnosticsLogicalContext.Push(context.Message.MessageId))
using (MappedDiagnosticsLogicalContext.SetScoped(Headers.ConversationId, "conversationId))
using (MappedDiagnosticsLogicalContext.SetScoped(Headers.CorrelationId, "correlationId))
await next().ConfigureAwait(false);
// Note: Make sure your format string has `${ndlc}` as a field.
var pipeline = endpointConfiguration.Pipeline;
pipeline.Register(behavior: new AssignMessageIdtoNLogNdlcBehavior(), description: "Assigns the incoming message id to the NLog NDLC.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.