From c6d2206cee801fa8ec037ae72ca3b3d1c9ed86ca Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Fri, 12 Jun 2026 09:44:55 +0300 Subject: [PATCH] refactor: improve code readability and remove duplication --- .../Handlers/DebugProbeHttpClientHandler.cs | 64 +++++++++---------- .../Internal/Utils/RedactionUtils.cs | 2 +- .../Middleware/DebugProbeMiddleware.cs | 25 ++++---- .../Storage/DebugEntryStore.cs | 7 +- 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs b/DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs index efaea0c..b7f606e 100644 --- a/DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs +++ b/DebugProbe.AspNetCore/Handlers/DebugProbeHttpClientHandler.cs @@ -50,19 +50,7 @@ protected override async Task SendAsync(HttpRequestMessage /// private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessage? response, Exception? exception, long durationMs) { - var context = _httpContextAccessor.HttpContext; - - if (context == null) - { - return; - } - - if (!context.Items.TryGetValue("DebugProbeEntry", out var value)) - { - return; - } - - if (value is not DebugEntry entry) + if (!TryGetActiveEntry(out var entry)) { return; } @@ -88,34 +76,42 @@ private async Task CaptureRequest(HttpRequestMessage request, HttpResponseMessag ResponseHeaders = response != null ? response.Headers.ToDictionary(x => x.Key, x => RedactionUtils.RedactHeader(x.Key, string.Join(", ", x.Value), _options)) : [] }; - if (request.Content != null) - { - var contentType = request.Content.Headers.ContentType?.MediaType; + outgoing.RequestBody = await CaptureBodyAsync(request.Content); - if (HttpContentUtils.IsTextContent(contentType)) - { - var body = await request.Content.ReadAsStringAsync(); + outgoing.ResponseBody = await CaptureBodyAsync(response?.Content); - outgoing.RequestBody = JsonUtils.Format(RedactionUtils.RedactJsonFields( - HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes), - _options)); - } - } + entry.OutgoingRequests.Add(outgoing); + } + + private bool TryGetActiveEntry(out DebugEntry entry) + { + entry = null!; + + var context = _httpContextAccessor.HttpContext; - if (response?.Content != null) + if (context == null || + !context.Items.TryGetValue("DebugProbeEntry", out var value) || + value is not DebugEntry activeEntry) { - var contentType = response.Content.Headers.ContentType?.MediaType; + return false; + } - if (HttpContentUtils.IsTextContent(contentType)) - { - var body = await response.Content.ReadAsStringAsync(); + entry = activeEntry; + return true; + } - outgoing.ResponseBody = JsonUtils.Format(RedactionUtils.RedactJsonFields( - HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes), - _options)); - } + private async Task CaptureBodyAsync(HttpContent? content) + { + if (content == null || + !HttpContentUtils.IsTextContent(content.Headers.ContentType?.MediaType)) + { + return string.Empty; } - entry.OutgoingRequests.Add(outgoing); + var body = await content.ReadAsStringAsync(); + + return JsonUtils.Format(RedactionUtils.RedactJsonFields( + HttpContentUtils.Trim(body, _options.MaxBodyCaptureSizeBytes), + _options)); } } diff --git a/DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs b/DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs index 8355923..c7e43e7 100644 --- a/DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs +++ b/DebugProbe.AspNetCore/Internal/Utils/RedactionUtils.cs @@ -98,7 +98,7 @@ private static string RedactQuery(string query, DebugProbeOptions options) continue; } - parts[i] = equalsIndex >= 0? $"{name}={options.RedactionText}" : $"{name}={options.RedactionText}"; + parts[i] = $"{name}={options.RedactionText}"; } return string.Join("&", parts); diff --git a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs index d3265d5..e262aed 100644 --- a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs +++ b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs @@ -57,16 +57,7 @@ public DebugProbeMiddleware(RequestDelegate next, DebugProbeOptions options) /// public async Task Invoke(HttpContext context, DebugEntryStore store) { - var path = context.Request.Path.Value ?? string.Empty; - - var ignorePaths = DefaultIgnorePaths - .Concat(_options.IgnorePaths) - .Distinct(StringComparer.OrdinalIgnoreCase); - - var ignored = ignorePaths.Any(x => - path.StartsWith(x, StringComparison.OrdinalIgnoreCase)); - - if (ignored) + if (IsIgnoredPath(context.Request.Path)) { await _next(context); return; @@ -116,7 +107,9 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) var statusCode = exception && context.Response.StatusCode == 200 ? 500 : context.Response.StatusCode; - var responseBody = exception ? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize) : CaptureResponseBody(context, responseCapture, maxBodySize); + var responseBody = exception + ? HttpContentUtils.Trim(exceptionResponseBody, maxBodySize) + : CaptureResponseBody(context, responseCapture, maxBodySize); entry.Method = context.Request.Method; @@ -162,6 +155,16 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) } } + private bool IsIgnoredPath(PathString requestPath) + { + var path = requestPath.Value ?? string.Empty; + + return DefaultIgnorePaths + .Concat(_options.IgnorePaths) + .Distinct(StringComparer.OrdinalIgnoreCase) + .Any(ignorePath => path.StartsWith(ignorePath, StringComparison.OrdinalIgnoreCase)); + } + private static async Task CaptureRequestBodyAsync(HttpContext context, int maxBodySize) { if (!HasBody(context.Request)) diff --git a/DebugProbe.AspNetCore/Storage/DebugEntryStore.cs b/DebugProbe.AspNetCore/Storage/DebugEntryStore.cs index caf9627..e9c0a8c 100644 --- a/DebugProbe.AspNetCore/Storage/DebugEntryStore.cs +++ b/DebugProbe.AspNetCore/Storage/DebugEntryStore.cs @@ -40,8 +40,11 @@ public DebugEntryStore(DebugProbeOptions options) public void Add(DebugEntry entry) { _queue.Enqueue(entry); + while (_queue.Count > _limit) + { _queue.TryDequeue(out _); + } } public List GetAll() @@ -59,7 +62,7 @@ public void Clear() while (_queue.TryDequeue(out _)) { } } - private string GetDateFormat() + private static string GetDateFormat() { var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; var index = shortDatePattern.LastIndexOf('y'); @@ -67,4 +70,4 @@ private string GetDateFormat() return dataFormat; } -} \ No newline at end of file +}