Add CorrelationId to Logger property for LogContext
Is there an existing issue for this?
- [x] I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
https://github.com/abpframework/abp/pull/16795
I see this PR already added the CorrelationId to the message in the event bus, but it did not automate add to the Logcontext, so the Log from the consumer handler didn't have the CorrelationId. It is hard to trace message process in consumer on the Log system (seq, kibana...)
Describe the solution you'd like
We need to manually add it to the handler by modified logger properties. This is my way
` public class ReceivedMessageEventHandler : IDistributedEventHandler<CategoryEto>, ITransientDependency { private ILogger<ReceivedMessageEventHandler> _logger; private ICorrelationIdProvider _correlationIdProvider;
public ReceivedMessageEventHandler(ILogger<ReceivedMessageEventHandler> logger, ICorrelationIdProvider correlationIdProvider)
{
_logger = logger;
_correlationIdProvider = correlationIdProvider;
var corrId = _correlationIdProvider.Get();
_logger.BeginScope(new Dictionary<string, object>
{
["CorrelationId"] = corrId
});
}
public async Task HandleEventAsync(CategoryEto eto)
{
var message = $"{eto.Id}: {eto.Name}";
_logger.LogWarning("Message OK {@eto}", eto);
}
} `
Additional context
I think your team can consider to add it in the logic of event bus incoming message. Thanks