CSharpEssentials.RequestResponseLogging
8.0.0
dotnet add package CSharpEssentials.RequestResponseLogging --version 8.0.0
NuGet\Install-Package CSharpEssentials.RequestResponseLogging -Version 8.0.0
<PackageReference Include="CSharpEssentials.RequestResponseLogging" Version="8.0.0" />
<PackageVersion Include="CSharpEssentials.RequestResponseLogging" Version="8.0.0" />
<PackageReference Include="CSharpEssentials.RequestResponseLogging" />
paket add CSharpEssentials.RequestResponseLogging --version 8.0.0
#r "nuget: CSharpEssentials.RequestResponseLogging, 8.0.0"
#:package CSharpEssentials.RequestResponseLogging@8.0.0
#addin nuget:?package=CSharpEssentials.RequestResponseLogging&version=8.0.0
#tool nuget:?package=CSharpEssentials.RequestResponseLogging&version=8.0.0
CSharpEssentials.RequestResponseLogging
Configurable middleware for logging HTTP requests and responses in ASP.NET Core.
Benchmarks against the framework's HttpLogging and Serilog.AspNetCore (request and response bodies of 1 KB to 1 MB, a metadata-only event per request) are in BENCHMARKS.md.
Features
- Logs request and response bodies, headers, path, method, timing and sizes.
LoggingOptionssets exactly which fields are logged.- Specific paths, such as health checks, can be ignored.
[SkipRequestLogging],[SkipResponseLogging]and[SkipRequestResponseLogging]control logging per endpoint.UseHandlerhands everyRequestResponseContextto your own code.
Installation
dotnet add package CSharpEssentials.RequestResponseLogging
Usage
Registration
using CSharpEssentials.RequestResponseLogging;
var app = builder.Build();
app.UseRouting();
app.AddRequestResponseLogging(opt =>
{
opt.UseLogger(app.Services.GetRequiredService<ILoggerFactory>(), LoggingOptions.CreateAllFields());
opt.IgnorePaths("/health", "/metrics");
});
app.MapControllers();
Without UseLogger (or UseHandler), the middleware runs but writes nothing.
Register it after UseRouting() when you call UseRouting() yourself. The skip attributes are read from the endpoint selected by routing, and a middleware that runs before routing sees no endpoint, so the attributes have no effect and the bodies are logged. (A WebApplication without an explicit UseRouting() adds routing at the start of the pipeline, so there it works anywhere.)
IgnorePaths matches by prefix, ignoring case and a trailing /: "/health" also skips /health/live and /healthz. "/" skips only the root path, and null or empty entries are skipped. Calling it again replaces the earlier list.
Each response write goes to the client right away and is also copied into memory for the log, so streamed responses still stream; the whole response body is held in memory until the request finishes. Use [SkipResponseLogging] (or, without a handler, leave Response and ResponseLength out of LoggingFields) on endpoints with very large responses. A request with a Content-Length over 10 MB is not read and is logged as Request body too large: {n} bytes; a body without a Content-Length (chunked) is read up to 10 MB, then logged as Request body too large: over 10485760 bytes, and the endpoint still receives the whole body; an empty body is logged as Empty request body, an empty response as Empty response body. If writing the response to the client fails, the exception is thrown inside the endpoint and handled like any other pipeline exception below. If the request pipeline throws, the middleware logs the captured request body with the exception type and message in the response field (Exception: {type}, Message: {message}), passes the same context to the UseHandler handler, and rethrows. A handler failure on this path is written to the logger as a second entry with Error writing log: {message} in the response field, and a logger failure is ignored, so the original exception still reaches the caller.
The level, fields and format come from LoggingOptions:
| Property | Default | Notes |
|---|---|---|
LoggingFields |
none | Request, Response, HostName, Path, Method, QueryString, Headers, ResponseTiming (mm:ss.fff), RequestLength, ResponseLength. LoggingOptions.CreateAllFields() selects all of them |
HeaderKeys |
none | The request headers written by the Headers field (HeaderKeys.Add("Accept-Language")). With no keys the field is empty, so credentials are not logged by default; with keys it is a list of the matching headers ([{"Accept-Language":"tr"}], [] when none is on the request). Names are compared case-sensitively with the spelling the server received: Kestrel normalizes the standard names (accept-language matches Accept-Language), but a custom header must match as sent (X-Correlation-Id does not match x-correlation-id, which is how HTTP/2 clients send it). Assign new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "X-Correlation-Id" } to ignore the case |
LoggingLevel |
Information |
|
LoggerCategoryName |
RequestResponseLogger |
|
UseSeparateContext |
true |
true writes one named placeholder per field ({Path}, {Method}, ...) so structured logging providers get each value as a property; false writes one formatted text |
RequestLength and ResponseLength are the number of characters of the logged body text, not bytes and not the Content-Length. The texts above count too, so a request without a body reports the 18 characters of Empty request body.
Bodies can contain personal data and secrets; log them only where that is acceptable, ignore sensitive paths and mark endpoints with the skip attributes.
Per-endpoint control
using CSharpEssentials.RequestResponseLogging;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("auth")]
public sealed class AuthController : ControllerBase
{
[HttpPost("login")]
[SkipRequestLogging] // don't log the request body (passwords)
public IActionResult Login(LoginRequest request) => Ok();
[HttpGet("large-data")]
[SkipResponseLogging] // don't log the response body
public IActionResult GetData() => Ok();
}
[SkipRequestResponseLogging] skips both. The attributes are read from endpoint metadata, so Minimal APIs use .WithMetadata(new SkipRequestLoggingAttribute()). A skipped body is logged as Skipped logging request body / Skipped logging response body.
Custom fields
app.AddRequestResponseLogging(opt =>
opt.UseLogger(app.Services.GetRequiredService<ILoggerFactory>(), logging =>
{
logging.LoggingFields = [LogFields.Method, LogFields.Path, LogFields.ResponseTiming];
logging.LoggingLevel = LogLevel.Debug;
}));
Custom handler
UseHandler receives the RequestResponseContext of every logged request after the rest of the pipeline has completed or thrown. It runs in addition to the logger when both are set. The context exposes Url, RequestBody, ResponseBody, ResponseCreationTime (a TimeSpan), ResponseTime (mm:ss.fff text), RequestLength and ResponseLength.
var logger = app.Services.GetRequiredService<ILogger<Program>>();
app.AddRequestResponseLogging(opt =>
{
opt.IgnorePaths("/health");
opt.UseHandler(context =>
{
logger.LogInformation("{Url} took {Time}, response {Length} characters", context.Url, context.ResponseTime, context.ResponseLength);
return Task.CompletedTask;
});
});
Upgrading to 8.0
- When the request pipeline throws, the handler set with
UseHandlernow runs before the exception is rethrown, so it sees failed requests too. The loggedRequestBodyholds the captured request body instead ofError occurred during request processing; the exception type and message are still inResponseBody. If your handler assumed every call was a completed request, checkResponseBodyfor theException:prefix. - The response is no longer buffered until the endpoint finishes. Each write goes to the client right away and is copied into the log, so streamed responses (server-sent events, large downloads, flushed chunks) reach the client as they are written. This changes four things:
- The first write or flush to
Response.Bodystarts the response. Set the status code, headers and cookies before it. Changing them afterwards now throwsInvalidOperationException(headers are read-only once the response has started), andResponse.OnStartingcallbacks run at the first write instead of when the endpoint returns. Before 8.0,await context.Response.WriteAsync("x"); context.Response.StatusCode = 201;worked; now the second line throws. - If writing to the client fails (for example the client disconnected), the exception is thrown inside the endpoint and reaches the caller. A single entry records it as
Exception: {type}, Message: {message}and goes to the logger and theUseHandlerhandler. Before 8.0 a single entry showedError capturing response body: {message}. Handle or filter the exception (often anIOExceptionorOperationCanceledException) where you handle aborted requests. - Once the endpoint has written part of the response, an exception thrown later can no longer be turned into a fresh error response by an outer exception handler, because the status code and headers are already sent. This is the same as running without the middleware.
- Synchronous writes and flushes to
Response.Body(for example disposing aStreamWriterover it) now reach the server directly, so they fail when the server does not allow synchronous IO (the Kestrel and IIS default). Before, the middleware's memory buffer accepted them. Write asynchronously, or setAllowSynchronousIO.
- The first write or flush to
IgnorePaths("/")now skips only the root path. Before, it skipped every request except the root, because"/"trimmed to an empty prefix that every path starts with. Anullor empty entry is now skipped instead of throwing or matching everything. To skip all requests, leave the middleware out.LoggingFieldsis now read once, whenAddRequestResponseLoggingruns. Adding or removing fields on theLoggingOptionsinstance after that has no effect. Before, the logger read the list on every request. Set the fields before you register the middleware.- Without a
UseHandlerhandler, the middleware now reads the request body only whenLoggingFieldscontainsRequestorRequestLength, and the response body only when it containsResponseorResponseLength. What gets logged is the same. When the request body is not read,Request.Bodyis left as the server gave it, so it is no longer seekable for later middleware or endpoints. If your code rewinds the request body, callRequest.EnableBuffering()yourself. With a handler, both bodies are still captured on every request. Microsoft.IO.RecyclableMemoryStreamis still installed with the package and used at runtime, but your code can no longer compile against it through this package. If you useRecyclableMemoryStreamManageror otherMicrosoft.IOtypes yourself, addMicrosoft.IO.RecyclableMemoryStreamto your project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
-
net11.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
-
net8.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
-
net9.0
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.0 | 132 | 11/9/2026 |
| 7.0.0 | 125 | 10/20/2026 |
| 6.5.2 | 109 | 10/10/2026 |
| 6.5.1 | 108 | 10/10/2026 |
| 6.5.0 | 117 | 10/9/2026 |
| 6.4.0 | 103 | 10/9/2026 |
| 6.3.0 | 100 | 10/8/2026 |
| 6.2.0 | 102 | 10/8/2026 |
| 6.1.0 | 98 | 10/7/2026 |
| 6.0.0 | 107 | 10/7/2026 |
| 5.2.1 | 110 | 10/7/2026 |
| 5.2.0 | 110 | 10/7/2026 |
| 5.1.0 | 103 | 10/7/2026 |
| 5.0.0 | 106 | 10/6/2026 |
| 4.1.0 | 114 | 10/6/2026 |
| 4.0.0 | 117 | 10/5/2026 |
| 3.2.3 | 177 | 5/31/2026 |
| 3.2.2 | 146 | 5/31/2026 |
| 3.2.1 | 136 | 5/31/2026 |
| 3.2.0 | 141 | 5/30/2026 |