Last active
March 25, 2021 16:57
-
-
Save ramonsmits/05f05bff19848d36d7e067ade4d3c024 to your computer and use it in GitHub Desktop.
NServiceBus NLog - Use incoming message id for the Nested Diagnostics Logical Context (NDLC)
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
class AssignMessageIdtoNLogNdlcBehavior : Behavior<ITransportReceiveContext> | |
{ | |
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next) | |
{ | |
var headers = context.Message.Headers; | |
headers.TryGetValue(Headers.ConversationId, out var conversationId); | |
headers.TryGetValue(Headers.CorrelationId, out var 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