-
Notifications
You must be signed in to change notification settings - Fork 24
Fix KeyNotFoundException in enrichers when accessing httpContext.Items #56
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
Conversation
…bility Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
…d of direct dictionary access Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
Co-authored-by: mo-esmp <1659032+mo-esmp@users.noreply.github.com>
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 replaces direct httpContext.Items
access with safe TryGetValue
calls to prevent KeyNotFoundException
in server-side Blazor and adds a test for the missing-IP scenario.
- Switched to
TryGetValue
in three enrichers (ClientIp
,CorrelationId
,ClientHeader
) - Added a new test for
ClientIpEnricher
when the key is absent - Verified no regressions and added broader hosting-model coverage
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
test/Serilog.Enrichers.ClientInfo.Tests/ClientIpEnricherTests.cs | Added test for missing ClientIp key scenario |
src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs | Switched to TryGetValue to safely read the item |
src/Serilog.Enrichers.ClientInfo/Enrichers/ClientIpEnricher.cs | Switched to TryGetValue to safely read the item |
src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs | Switched to TryGetValue to safely read the item |
Comments suppressed due to low confidence (2)
src/Serilog.Enrichers.ClientInfo/Enrichers/CorrelationIdEnricher.cs:47
- Add a unit test for
CorrelationIdEnricher
to verify that it does not throw and correctly falls back when theCorrelationIdItemKey
is missing fromhttpContext.Items
.
if (httpContext.Items.TryGetValue(CorrelationIdItemKey, out var value) && value is LogEventProperty logEventProperty)
src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs:49
- Add a unit test for
ClientHeaderEnricher
to ensure it handles a missing header item gracefully (no exception) and applies the correct fallback behavior.
if (httpContext.Items.TryGetValue(_clientHeaderItemKey, out var value) && value is LogEventProperty logEventProperty)
@@ -38,7 +38,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | |||
|
|||
var ipAddress = httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"; | |||
|
|||
if (httpContext.Items[IpAddressItemKey] is LogEventProperty logEventProperty) | |||
if (httpContext.Items.TryGetValue(IpAddressItemKey, out var value) && value is LogEventProperty logEventProperty) |
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.
[nitpick] The TryGetValue
+cast logic is duplicated across enrichers; consider extracting a shared helper or base-method to reduce duplication and improve maintainability.
if (httpContext.Items.TryGetValue(IpAddressItemKey, out var value) && value is LogEventProperty logEventProperty) | |
if (TryGetLogEventProperty(httpContext.Items, IpAddressItemKey, out var logEventProperty)) |
Copilot uses AI. Check for mistakes.
The enrichers were causing
KeyNotFoundException
when trying to access keys that don't exist inhttpContext.Items
. This was particularly problematic in Blazor server-side circuits where the HttpContext lifecycle differs from traditional HTTP requests.Root Cause
The code was using direct dictionary access which throws exceptions for missing keys:
Solution
Replaced direct dictionary access with safe
TryGetValue()
calls:Files Fixed
ClientIpEnricher.cs
- Fixed access toSerilog_ClientIp
keyCorrelationIdEnricher.cs
- Fixed access toSerilog_CorrelationId
keyClientHeaderEnricher.cs
- Fixed access to header-specific keysTesting
127.0.0.1
test-123
TestAgent/1.0
This fix ensures the enrichers work reliably across all ASP.NET Core hosting models, including Blazor server-side circuits where this issue was most commonly reported.
Fixes #49.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.