Csharpessentials logging

From senrecep/CSharpEssentials · View interactive version

Csharpessentials logging is an AI agent skill from the GitHub repository senrecep/CSharpEssentials. Use when logging HTTP requests and responses in ASP.NET Core. Covers app.AddRequestResponseLogging(opt => …) with UseLogger(ILoggerFactory, LoggingOptions), UseHandler, IgnorePaths, LogFields selection, and [SkipRequestLogging] / [SkipResponseLogging] / [SkipRequestResponseLogging] endpoint metadata

How to use this skill

Skill file

CSharpEssentials.RequestResponseLogging

Middleware that logs HTTP requests and responses (bodies, headers, path, method, timing, sizes). Configured on IApplicationBuilder; no service registration is needed.

Installation

dotnet add package CSharpEssentials.RequestResponseLogging

Namespace

using CSharpEssentials.RequestResponseLogging;

Register

AddRequestResponseLogging is an IApplicationBuilder extension that adds the middleware. Call it on the built app.

var app = builder.Build();

app.AddRequestResponseLogging(opt =>
{
    opt.UseLogger(app.Services.GetRequiredService<ILoggerFactory>(), LoggingOptions.CreateAllFields());
    opt.IgnorePaths("/health", "/metrics");
});

Choosing fields

app.AddRequestResponseLogging(opt =>
    opt.UseLogger(app.Services.GetRequiredService<ILoggerFactory>(), logging =>
    {
        logging.LoggingFields = [LogFields.Method, LogFields.Path, LogFields.ResponseTiming];
        logging.HeaderKeys = ["X-Correlation-Id"];
        logging.LoggingLevel = LogLevel.Debug;
        logging.LoggerCategoryName = "Http";
    }));

LogFields: Request, Response, HostName, Path, Method, QueryString, Headers, ResponseTiming, RequestLength, ResponseLength. LoggingOptions.CreateAllFields() selects all of them; the default list is empty.


Custom handler

app.AddRequestResponseLogging(opt => opt.UseHandler(context =>
{
    Console.WriteLine($"{context.Url} {context.ResponseTime} {context.RequestLength} {context.ResponseLength}");
    return Task.CompletedTask;
}));

RequestResponseContext exposes RequestBody, ResponseBody, ResponseCreationTime (TimeSpan?), ResponseTime (mm:ss.fff text), RequestLength, ResponseLength and Url. The handler runs after the rest of the pipeline has completed or thrown.


Per-endpoint opt-out

The middleware reads the attributes from endpoint metadata:

| Attribute | Effect |

|---|---|

| [SkipRequestLogging] | Request body is not captured |

| [SkipResponseLogging] | Response body is not captured |

| [SkipRequestResponseLogging] | Neither body is captured |

[ApiController]
[Route("auth")]
public sealed class AuthController : ControllerBase
{
    [HttpPost("login")]
    [SkipRequestLogging] // passwords
    public IActionResult Login(LoginRequest request) => Ok();
}

// Minimal APIs
app.MapGet("/export", () => Results.Ok()).WithMetadata(new SkipResponseLoggingAttribute());

Best practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.