'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.
Solution 1:[1]
The HttpContext.TraceIdentifier Property: Gets or sets a unique identifier to represent this request in trace logs.
The Activity.Current Property: Gets or sets the current operation (Activity) for the current thread.
var traceId = Activity.Current?.Id ?? httpContext?.TraceIdentifier;
From the source code, we can see that it will use the Activity Id as the first choice, if the Activity Id is null, it will use the httpContext?.TraceIdentifier, you can also use this method to set the trace id.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zhi Lv |
