CSharpEssentials.RequestResponseLogging 8.0.0

dotnet add package CSharpEssentials.RequestResponseLogging --version 8.0.0
                    
NuGet\Install-Package CSharpEssentials.RequestResponseLogging -Version 8.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpEssentials.RequestResponseLogging" Version="8.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.RequestResponseLogging" Version="8.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.RequestResponseLogging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.RequestResponseLogging --version 8.0.0
                    
#r "nuget: CSharpEssentials.RequestResponseLogging, 8.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSharpEssentials.RequestResponseLogging@8.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.RequestResponseLogging&version=8.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.RequestResponseLogging&version=8.0.0
                    
Install as a Cake Tool

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.
  • LoggingOptions sets exactly which fields are logged.
  • Specific paths, such as health checks, can be ignored.
  • [SkipRequestLogging], [SkipResponseLogging] and [SkipRequestResponseLogging] control logging per endpoint.
  • UseHandler hands every RequestResponseContext to 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 UseHandler now runs before the exception is rethrown, so it sees failed requests too. The logged RequestBody holds the captured request body instead of Error occurred during request processing; the exception type and message are still in ResponseBody. If your handler assumed every call was a completed request, check ResponseBody for the Exception: 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.Body starts the response. Set the status code, headers and cookies before it. Changing them afterwards now throws InvalidOperationException (headers are read-only once the response has started), and Response.OnStarting callbacks 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 the UseHandler handler. Before 8.0 a single entry showed Error capturing response body: {message}. Handle or filter the exception (often an IOException or OperationCanceledException) 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 a StreamWriter over 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 set AllowSynchronousIO.
  • 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. A null or empty entry is now skipped instead of throwing or matching everything. To skip all requests, leave the middleware out.
  • LoggingFields is now read once, when AddRequestResponseLogging runs. Adding or removing fields on the LoggingOptions instance after that has no effect. Before, the logger read the list on every request. Set the fields before you register the middleware.
  • Without a UseHandler handler, the middleware now reads the request body only when LoggingFields contains Request or RequestLength, and the response body only when it contains Response or ResponseLength. What gets logged is the same. When the request body is not read, Request.Body is left as the server gave it, so it is no longer seekable for later middleware or endpoints. If your code rewinds the request body, call Request.EnableBuffering() yourself. With a handler, both bodies are still captured on every request.
  • Microsoft.IO.RecyclableMemoryStream is still installed with the package and used at runtime, but your code can no longer compile against it through this package. If you use RecyclableMemoryStreamManager or other Microsoft.IO types yourself, add Microsoft.IO.RecyclableMemoryStream to your project.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed