Last active
November 8, 2018 10:07
-
-
Save ramonsmits/5c1363f7bbb1df122d5e116f9786f877 to your computer and use it in GitHub Desktop.
NServiceBus - Add message context identifiers to exception;
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
// | |
// 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