question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] `HttpContext.TraceIdentifier` vs `Activity.Current.Id`

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
adityamandaleekacommented, Feb 1, 2022

@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.).

0reactions
lonix1commented, Apr 11, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >
Handle errors in ASP.NET Core
Discover how to handle errors in ASP.NET Core apps.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found