Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions DebugProbe.AspNetCore/Middleware/DebugProbeMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Text;
using DebugProbe.AspNetCore.Models;
using DebugProbe.AspNetCore.Options;
Expand Down Expand Up @@ -58,14 +58,18 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
var started = Stopwatch.StartNew();

var exception = false;
string? exceptionMessage = null;
string? exceptionStackTrace = null;

try
{
await _next(context);
}
catch
catch (Exception ex)
{
exception = true;
exceptionMessage = ex.Message;
exceptionStackTrace = ex.StackTrace;
throw;
}
finally
Expand Down Expand Up @@ -100,25 +104,25 @@ public async Task Invoke(HttpContext context, DebugEntryStore store)
$"{context.Request.Path}{context.Request.QueryString}",
RequestBody = Trim(requestBody),


// Response
ResponseBody = Trim(responseBody),

// Exception
ExceptionMessage = exceptionMessage,
ExceptionStackTrace = Trim(exceptionStackTrace, max: 4000),

// Headers
Headers = context.Request.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()),


// Other
Timestamp = DateTime.UtcNow,

});
}
}

private string Trim(string value, int max = 2000)
private string Trim(string? value, int max = 2000)
{
if (string.IsNullOrEmpty(value)) return value;
if (string.IsNullOrEmpty(value)) return value ?? string.Empty;
return value.Length <= max ? value : value.Substring(0, max);
}
}
}
8 changes: 6 additions & 2 deletions DebugProbe.AspNetCore/Models/DebugEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DebugProbe.AspNetCore.Models;
namespace DebugProbe.AspNetCore.Models;

public class DebugEntry
{
Expand All @@ -19,9 +19,13 @@ public class DebugEntry
public long ResponseSize { get; set; }
public string ResponseBody { get; set; } = default!;

// Exception (captured when middleware catches an unhandled error)
public string? ExceptionMessage { get; set; }
public string? ExceptionStackTrace { get; set; }

// Headers
public Dictionary<string, string> Headers { get; set; } = new();

// Metadata
public DateTimeOffset Timestamp { get; set; }
}
}