Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HttpContext.TraceIdentifier and Activity.Current.Id

I use a "trace id" in logs. Advice on the aspnet repo and here on SO is to get a traceid from HttpContext.TraceIdentifier.

But when the framework creates a ProblemDetails for a validation failure, it uses Activity.Current?.Id.

Examples:

var traceId1 = Activity.Current?.Id;        // OWZ4G27FO6UWH:00000003
var traceId2 = HttpContext.TraceIdentifier; // 00-2a8ee37903e657e3a95b41178dafc56e-91b02006afcf3133-00

I'm worried that my logs use one type of trace id, and the framework uses another - and when I find myself needing to analyse logs I'll have a problem.

How do these differ? Which should I use? Which type does the framework typically use?

UPDATE

I asked on the repo too, and they closed it without explanation. If you have time, please open a new issue, and give us the link so we can upvote your issue.

like image 884
lonix Avatar asked Dec 08 '25 14:12

lonix


1 Answers

Here is an idea - that may hold water:

  • Tasks where the aspnetcore is failing may or may not be associated with a HTTP request - but rather the Activity.Current?.Id is more relevant - what sub-action failed?
  • Users of aspnetcore are often more concerned about which http request failed than which aspect of the underlying framework, hence the advice for httpContext?.TraceIdentifier.

Therefore, the appearance of a discrepancy may just be a different perspective from the core libraries and the custom server code as to what is the most appropriate ID. So, most users should probably just follow the standard advice and use httpContext?.TraceIdentifier, or likely:

// Use http first to trace to client call, if available
// If not, fall back on the thread ID
var traceId = httpContext?.TraceIdentifier ?? Activity.Current?.Id;
like image 97
johnml1135 Avatar answered Dec 12 '25 06:12

johnml1135



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!