diff --git a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs index 9709b86..609f1bf 100644 --- a/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs +++ b/DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs @@ -44,10 +44,17 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) var started = Stopwatch.StartNew(); + var exception = false; + try { await _next(context); } + catch + { + exception = true; + throw; + } finally { started.Stop(); @@ -55,14 +62,16 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) ms.Position = 0; var responseBody = await new StreamReader(ms).ReadToEndAsync(); ms.Position = 0; - await ms.CopyToAsync(originalBody); + 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 statusCode = exception && context.Response.StatusCode == 200 ? 500 : context.Response.StatusCode; + store.Add(new DebugEntry { Id = Guid.NewGuid().ToString(), @@ -80,7 +89,7 @@ public async Task Invoke(HttpContext context, DebugEntryStore store) Method = context.Request.Method, Path = context.Request.Path, Query = context.Request.QueryString.ToString(), - StatusCode = context.Response.StatusCode, + StatusCode = statusCode, RequestTimeUtc = DateTime.UtcNow, DurationMs = started.ElapsedMilliseconds, RequestSize = Encoding.UTF8.GetByteCount(requestBody),