[Question] `HttpContext.TraceIdentifier` vs `Activity.Current.Id`
See original GitHub issueI asked this on SO first. There are no relevant docs.
I use a “trace id” in logs. Advice here, on the docs site and 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; // 00-2a8ee37903e657e3a95b41178dafc56e-91b02006afcf3133-00
var traceId2 = HttpContext.TraceIdentifier; // OWZ4G27FO6UWH:00000003
I’m worried that my logs use one type, and the framework uses another - and when I need to analyse logs I’ll have a problem.
Which does the framework typically use? And which should I use?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Difference between HttpContext.TraceIdentifier and Activity ...
The HttpContext.TraceIdentifier Property: Gets or sets a unique identifier to represent this request in trace logs. The Activity.Current Property: Gets or sets ...
Read more >Activity Enrichment in ASP.NET Core 6.0
Current property isn't the activity of the incoming web request, but something else altogether. If you're intending to add details to the ...
Read more >Use GUID as TraceIdentifier in ASP.NET Core Web API
TraceIdentifier is a unique identifier to represent a request in trace logs. Kestrel generates this ID as {ConnectionId}:{Request number} like ...
Read more >HttpContext.TraceIdentifier Property
Gets or sets a unique identifier to represent this request in trace logs.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lonix1 Activity is a logical concept that can be defined at whatever granularity makes sense for the use case. The idea is that you can have one unit of tracking that spans as much as you need it to for the purpose of your diagnostics.
HttpContext’s TraceIdentifier, on the other hand, is for a specific request.
As a concrete example, if you had a social networking site with multiple backend services, you may choose to define “load a user’s profile page” as an Activity, and have many requests to the various services happen in the context of that Activity.
Therefore the answer to “which should I use” is really dependent on how you use Activity in your use case, and your needs for how to consume the logged information (do you need per-request granularity? do you want logs for a logical action across multiple requests to be easily correlated? etc.).
This shouldn’t be locked - it hasn’t been addressed. And I don’t know what feedback I need to provide (this was closed because of that Author Feedback tag), it’s MS that should provide feedback.