I am having a .net core console app which is a listener for RabbitMQ. Also I am using Serilog for logging. The API which pushes message into the queue adds a Correlation ID as one parameter in the message. While writing the logs, I want the CorrelationID (as specified in the output template of serilog config) to be replaced with the one available in the message, to track the operation end to end. Can anybody suggest a way to achieve this?
Note: If it is a web request, Serilog provided middlewares (Scoped Middleware or LogContext) come to the rescue. However, they require HttpContext (for obvious reasons). For this kind of listener request, I am stuck to find a solution
Any help is much appreciated
At the point where you pick up the message, you should be able to to
using (LogContext.PushProperty("corrId", msg.CorrId))
ProcessMessage(msg)
You can also add a ....|{Properties}
element into your log format string to pick up any properties not used in the message template associated with a given message (but it sounds like you have that covered)