Skip to content

Instantly share code, notes, and snippets.

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/5c1363f7bbb1df122d5e116f9786f877 to your computer and use it in GitHub Desktop.
Save ramonsmits/5c1363f7bbb1df122d5e116f9786f877 to your computer and use it in GitHub Desktop.
NServiceBus - Add message context identifiers to exception;
//
// var pipeline = endpointConfiguration.Pipeline;
// pipeline.Register(behavior: new AddMessageContextIdentifierstoExceptionBehavior(), description: "Assigns the incoming message context identifiers to the exception for diagnostical purposes.");
//
class AddMessageContextIdentifierstoExceptionBehavior : Behavior<ITransportReceiveContext>
{
public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{
try
{
await next().ConfigureAwait(false);
}
catch(Exception ex)
{
ex.Data[Headers.MessageId] = context.Message.MessageId;
var headers = context.Message.Headers;
if(headers.TryGet(Headers.ConversationId, out string conversationId)) ex.Data[Headers.ConversationId] = conversationId;
if(headers.TryGet(Headers.CorrelationId, out string correlationId)) ex.Data[Headers.CorrelationId] = correlationId;
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment