code4it-dev / blog-comments

https://www.code4it.dev/
1 stars 0 forks source link

blog/serilog-correlation-id #41

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

How to log Correlation IDs in .NET APIs with Serilog - Code4IT

APIs often call other APIs to perform operations. If an error occurs in one of them, how can you understand the context that caused that error? You can use Correlation IDs in your logs!

https://www.code4it.dev/blog/serilog-correlation-id

nparekh12 commented 1 year ago

Hi David, nicely explained. Thank you very much. I couldn't find the LogCorrelationId Demo on Github. the link is not working. do you still have it? I want to see what you did on Target application. How did you retrieve correlation id passed from source and logged in Serilog.

bellons91 commented 1 year ago

Ouch, I totally forgot to make it public!

Here you go: https://github.com/code4it-dev/LogCorrelationId

catokx commented 9 months ago

My "booking" service is fired by an external system webhook, and i cannot add the correlation id header to the request (and i also want that it is random as the default correlation id of serilog), how can i "generate" the correlation id in the booking microservice and propagate it to the other services?

bellons91 commented 9 months ago

@catokx, if I got it right you have an external webhook that calls your systems without passing a correlationId, right?

Keeping the example above, the structure is similar but instead of having Client you have an Webhook not under your control?

catokx commented 8 months ago

Yes, i need that the api gateway creates its correlationId if it is not present in the request header (as it already do) and then propagate the newly created correlation id to the other services. I tried it with the demo code and in a lot of different configurations following your guide and the demo code, but the correlationId is propagated to the other services only if it is passed in the original request.

catokx commented 7 months ago

@bellons91 i temporary solved the problem creating a correlation id header in the load balancer, so the first microservice receives it in the header and propagates it to the other microservices, but it is strange that if no header is provided, it is still created but not propagated.

zing-rsa commented 6 months ago

@catokx I had the same question. Adding this incase anyone else comes across. I solved this with a custom approach partly based on another article on this site. I created some custom middleware that checks the HttpContext for a correlationId header, and if not found, creates one. It then adds it to HttpContext.Items. Then to propagate the header, I adapted the AddHeaderPropagation from this article, to check for the correlationId in the HttpContext.Items, and propegate it to all the HttpClients.

catokx commented 6 months ago

@catokx I had the same question. Adding this incase anyone else comes across. I solved this with a custom approach partly based on another article on this site. I created some custom middleware that checks the HttpContext for a correlationId header, and if not found, creates one. It then adds it to HttpContext.Items. Then to propagate the header, I adapted the AddHeaderPropagation from this article, to check for the correlationId in the HttpContext.Items, and propegate it to all the HttpClients.

I think it should be an embedded fetature, anyway i solved it in a different manner. I configured the load balancer (nginx) to gnerate the correlation id and add it to the request header. It now works as expected.