Rebus - Execute action before message is handled

179 views Asked by At

Is there a way on Rebus of executing an action before the IHandleMessages.Handle is called?

Particulary I want to set a correlation id of my logs for log4net and I'm having really difficult times to get this working.

I have an activity pattern converter to get a Guid from

System.Diagnostics.Trace.CorrelationManager.ActivityId = Guid.NewGuid()

But I want to be able to set this before each handle is activated. I'm using Autofac as container with Rebus

1

There are 1 answers

10
mookid8000 On BEST ANSWER

An easy and convenient way to achieve this would be to include the Rebus.Events package, which allows for setting up an event delegate to be invoked before each message is handled like this:

Configure.With(...)
    .(...)
    .Events(e =>
    {
        e.BeforeMessageHandled += (bus, headers, message, context, args) =>
        {
            // do what you want in here :)
        };
    });