From fe23c850656121e445f37dafaa0ea01283b2a758 Mon Sep 17 00:00:00 2001 From: Lucy Batten Date: Sat, 9 May 2026 14:29:35 +0100 Subject: [PATCH] fix: fix missing request persistence on unhandled exceptions --- .../Middleware/DebugProbeMiddleware.cs | 89 ++++++++++--------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs index 745ad2e..9709b86 100644 --- a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs +++ b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs @@ -44,60 +44,67 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) var started = Stopwatch.StartNew(); - await _next(context); + try + { + await _next(context); + } + finally + { + started.Stop(); - started.Stop(); + ms.Position = 0; + var responseBody = await new StreamReader(ms).ReadToEndAsync(); + ms.Position = 0; + await ms.CopyToAsync(originalBody); - ms.Position = 0; - var responseBody = await new StreamReader(ms).ReadToEndAsync(); - ms.Position = 0; - await ms.CopyToAsync(originalBody); + context.Response.Body = originalBody; - var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; - var index = shortDatePattern.LastIndexOf('y'); - var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern; + var shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; + var index = shortDatePattern.LastIndexOf('y'); + var dataFormat = index >= 0 ? shortDatePattern[..(index + 1)] : shortDatePattern; - store.Add(new DebugEntry - { - Id = Guid.NewGuid().ToString(), + store.Add(new DebugEntry + { + Id = Guid.NewGuid().ToString(), - // Environment - Environment = EnvironmentUtils.TryGetEnvironment(), - MachineName = Environment.MachineName, - AssemblyVersion = Assembly.GetEntryAssembly()?.GetName().Version?.ToString(), - TimeZone = TimeZoneInfo.Local.DisplayName, - Culture = CultureInfo.CurrentCulture.Name, - DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, - DateFormat = dataFormat, + // Environment + Environment = EnvironmentUtils.TryGetEnvironment(), + MachineName = Environment.MachineName, + AssemblyVersion = Assembly.GetEntryAssembly()?.GetName().Version?.ToString(), + TimeZone = TimeZoneInfo.Local.DisplayName, + Culture = CultureInfo.CurrentCulture.Name, + DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, + DateFormat = dataFormat, - // Overview - Method = context.Request.Method, - Path = context.Request.Path, - Query = context.Request.QueryString.ToString(), - StatusCode = context.Response.StatusCode, - RequestTimeUtc = DateTime.UtcNow, - DurationMs = started.ElapsedMilliseconds, - RequestSize = Encoding.UTF8.GetByteCount(requestBody), - ResponseSize = Encoding.UTF8.GetByteCount(responseBody), + // Overview + Method = context.Request.Method, + Path = context.Request.Path, + Query = context.Request.QueryString.ToString(), + StatusCode = context.Response.StatusCode, + RequestTimeUtc = DateTime.UtcNow, + DurationMs = started.ElapsedMilliseconds, + RequestSize = Encoding.UTF8.GetByteCount(requestBody), + ResponseSize = Encoding.UTF8.GetByteCount(responseBody), - // Request - RequestUrl = $"{context.Request.Scheme}://{context.Request.Host}" + - $"{context.Request.Path}{context.Request.QueryString}", - RequestBody = Trim(requestBody), + // Request + RequestUrl = $"{context.Request.Scheme}://{context.Request.Host}" + + $"{context.Request.Path}{context.Request.QueryString}", + RequestBody = Trim(requestBody), - // Response - ResponseBody = Trim(responseBody), + // Response + ResponseBody = Trim(responseBody), - // Headers - Headers = context.Request.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()), + // Headers + Headers = context.Request.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()), - // Other - Timestamp = DateTime.UtcNow, - - }); + // Other + Timestamp = DateTime.UtcNow, + + }); + } } private string Trim(string value, int max = 2000)