-
Notifications
You must be signed in to change notification settings - Fork 24
Add HttpContext extension method for graceful correlation ID retrieval #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
Would it be a good idea to move the correlation ID generation logic to the |
I considered this approach but decided against it for several key reasons: 1. Separation of Concerns: The 2. Respects User Configuration: When users configure 3. Principle of Least Surprise: A "Get" method should retrieve existing values, not generate new ones. Users expect deterministic behavior - the same call should return the same result. 4. Consistency: The extension method returns exactly what appears in the logs, ensuring log correlation IDs match what users retrieve via If you need guaranteed non-null values, I'd recommend either:
This keeps the API predictable and respects the configured enrichment behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a straightforward extension for retrieving correlation IDs from HttpContext
, updates the enricher to cache string values for those IDs, aligns project TFMs to .NET 8.0, and adds tests and docs for the new API.
- Added
HttpContext.GetCorrelationId()
extension method - Updated
CorrelationIdEnricher
to store a string correlation ID alongside theLogEventProperty
- Adjusted target frameworks to net8.0, added new tests, and documented usage in README
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
test/Serilog.Enrichers.ClientInfo.Tests/Serilog.Enrichers.ClientInfo.Tests.csproj | Downgraded test project to net8.0 and updated package versions |
test/Serilog.Enrichers.ClientInfo.Tests/CorrelationIdEnricherTests.cs | Added tests for all GetCorrelationId scenarios and backward compatibility |
src/Serilog.Enrichers.ClientInfo/Serilog.Enrichers.ClientInfo.csproj | Removed net9.0 TFM, now targets only net8.0 |
src/Serilog.Enrichers.ClientInfo/Extensions/HttpContextExtensions.cs | Introduced extension to fetch correlation ID from HttpContext.Items |
src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs | Caches the string value for the correlation ID in HttpContext.Items |
README.md | Documented GetCorrelationId usage in a new “Retrieving Correlation ID” section |
Comments suppressed due to low confidence (3)
README.md:98
- The example calls
HttpContext.GetCorrelationId()
but doesn’t show the requiredusing Serilog.Enrichers;
directive. Add this import to the snippet so users know how to reference the extension.
#### Retrieving Correlation ID
src/Serilog.Enrichers.ClientInfo/Serilog.Enrichers.ClientInfo.csproj:3
- Dropping the
net9.0
target framework may break consumers on .NET 9. If backward compatibility is required, consider multi-targeting again or document this change as a breaking update in your release notes.
<TargetFrameworks>net8.0</TargetFrameworks>
test/Serilog.Enrichers.ClientInfo.Tests/Serilog.Enrichers.ClientInfo.Tests.csproj:4
- [nitpick] The test project now only targets
net8.0
. If you plan to support multiple TFMs, you might want to run tests against those frameworks as well to ensure coverage.
<TargetFramework>net8.0</TargetFramework>
@@ -9,6 +9,7 @@ namespace Serilog.Enrichers; | |||
public class CorrelationIdEnricher : ILogEventEnricher | |||
{ | |||
private const string CorrelationIdItemKey = "Serilog_CorrelationId"; | |||
private const string CorrelationIdValueKey = "Serilog_CorrelationId_Value"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key "Serilog_CorrelationId_Value" is duplicated in both the enricher and the extension. Extract it into a shared constant or static class to avoid mismatches and ease future maintenance.
Copilot uses AI. Check for mistakes.
This PR adds a clean, graceful way to retrieve correlation IDs from
HttpContext
as requested in issue #51, eliminating the need for manual casting and complex code.Problem
Previously, users had to use hacky code to retrieve correlation IDs for error reporting:
Solution
Added a clean
HttpContext
extension method that provides direct access to correlation IDs:Changes Made
HttpContextExtensions.GetCorrelationId()
for clean API accessCorrelationIdEnricher
to store correlation ID as bothLogEventProperty
(existing) and string value (new)Key Benefits
null
when correlation ID is not availableTesting
All existing tests continue to pass, plus new comprehensive tests ensure:
null
appropriately when no correlation ID existsFixes #51.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.