Skip to content

Commit b2b05f9

Browse files
committed
Enhance logging to handle non-JSON requests and responses gracefully
1 parent 9f78b9c commit b2b05f9

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/SharedKernel/Logging/ReductionHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public static object ParseAndRedactJson(string body)
7171
}
7272
catch (JsonException)
7373
{
74-
// If invalid JSON, do naive string-based redaction
75-
return RedactSensitiveString(body);
74+
return (Headers: "{}", Body: "[SKIPPED_NON_JSON]");
7675
}
7776
}
7877

@@ -135,7 +134,9 @@ private static Dictionary<string, object> RedactObject(JsonElement element)
135134
private static string RedactSensitiveString(string? value)
136135
{
137136
if (string.IsNullOrWhiteSpace(value))
137+
{
138138
return string.Empty;
139+
}
139140

140141
return SensitiveKeywords.Any(k => value.Contains(k, StringComparison.OrdinalIgnoreCase))
141142
? "[REDACTED]"

src/SharedKernel/Logging/RequestLoggingMiddleware.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ internal sealed class RequestLoggingMiddleware(
1515
"/above-board"
1616
];
1717

18+
private static readonly HashSet<string> JsonMediaTypes = new(StringComparer.OrdinalIgnoreCase)
19+
{
20+
"application/json",
21+
"text/json"
22+
};
23+
1824
public async Task InvokeAsync(HttpContext context)
1925
{
2026
if (HttpMethods.IsOptions(context.Request.Method))
@@ -30,7 +36,12 @@ public async Task InvokeAsync(HttpContext context)
3036
return;
3137
}
3238

33-
var requestLog = await CaptureRequestAsync(context.Request);
39+
var isJsonRequest = IsJson(context.Request.ContentType);
40+
41+
var requestLog = isJsonRequest
42+
? await CaptureRequestAsync(context.Request)
43+
: (Headers: "{}", Body: "[SKIPPED_NON_JSON]");
44+
3445
var originalBodyStream = context.Response.Body;
3546

3647
await using var responseBody = new MemoryStream();
@@ -46,7 +57,12 @@ public async Task InvokeAsync(HttpContext context)
4657
{
4758
var elapsedMs = Stopwatch.GetElapsedTime(stopwatch)
4859
.TotalMilliseconds;
49-
var responseLog = await CaptureResponseAsync(context.Response);
60+
61+
var isJsonReply = IsJson(context.Response.ContentType);
62+
63+
var responseLog = isJsonReply
64+
? await CaptureResponseAsync(context.Response)
65+
: (Headers: "{}", Body: "[SKIPPED_NON_JSON]");
5066

5167
logger.LogInformation(
5268
"[Incoming Request] HTTP {Method} {Query} responded with {StatusCode} in {ElapsedMilliseconds}ms. " +
@@ -95,4 +111,9 @@ public async Task InvokeAsync(HttpContext context)
95111
JsonSerializer.Serialize(redactedBody)
96112
);
97113
}
114+
115+
private static bool IsJson(string? contentType) =>
116+
!string.IsNullOrWhiteSpace(contentType) &&
117+
(JsonMediaTypes.Any(contentType.StartsWith) ||
118+
contentType.Contains("+json", StringComparison.OrdinalIgnoreCase));
98119
}

src/SharedKernel/SharedKernel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.3.6</Version>
11+
<Version>1.3.7</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>Package updates</PackageReleaseNotes>
17+
<PackageReleaseNotes>Logging changed to work only with json requests and responses</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)