如何使用Serilog记录MassTransit CorrelationId? [英] How to log MassTransit CorrelationId with Serilog?

查看:83
本文介绍了如何使用Serilog记录MassTransit CorrelationId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MassTransit 3.5.7.我在启动总线之前添加了相关ID:

I am using MassTransit 3.5.7. I add the correlation ids before starting the bus:

MessageCorrelation.UseCorrelationId<ICustomerRegistered>(x => x.CustomerGuid);
MessageCorrelation.UseCorrelationId<ICustomerUpdated>(x => x.Customer.Guid);
MessageCorrelation.UseCorrelationId<IOrderPaid>(x => x.OrderGuid);
MessageCorrelation.UseCorrelationId<IOrderPaymentAuthorized>(x => x.OrderGuid);
MessageCorrelation.UseCorrelationId<IOrderPlaced>(x => x.Order.OrderGuid);
MessageCorrelation.UseCorrelationId<IOrderRefunded>(x => x.OrderGuid);

我使用Serliog 2.5.0进行记录:

I use Serliog 2.5.0 for logging:

Bus.Factory.CreateUsingAzureServiceBus(serviceBus =>
{
    serviceBus.UseSerilog();
    ...
}

我可以看到CorrelationId已添加到消息标题中,但没有作为自定义属性添加到日志中.

I can see that the CorrelationId is added to the message headers, but it's not added as a custom property to the logs.

要为所有已记录的消息记录CorrelationId,我需要做什么?

What do I need to do in order to log the CorrelationId for all logged messages?

更新:

我实现了Serilog浓缩器和MassTransit自定义中间件,例如此处,但是相关ID仅记录接收(消耗)的消息.发布消息时未记录.有谁知道发布消息时如何记录相关ID?

I implemented a Serilog enricher and a MassTransit custom middleware like here, but the correlation id is only logged in the received (consumed) messages. It is not logged when publishing a message. Does anyone know how can I log the correlation id when publishing a message?

推荐答案

您可以很轻松地实现此目标,而且正确的是,它是使用您自己的中间件实现的.

You can Achieve this quite easily, and you are correct, it's with your own middleware.

我只是在例子.现在,我正在使用LibLog(因此您将看到LogProvider).但是,如果您直接引用SeriLog,则只需执行 LogContext.PushProperty(...),您仍然会获得相同的效果

I did that just that in this example. Now I am using LibLog (hence you will see LogProvider). But if you are referencing SeriLog directly, then you can simply do LogContext.PushProperty(...), and you will still get the same effect

这篇关于如何使用Serilog记录MassTransit CorrelationId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆