Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active March 25, 2021 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/05f05bff19848d36d7e067ade4d3c024 to your computer and use it in GitHub Desktop.
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)
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