In ASP.NET Core 10, how do you enable HTTP logging, what does it log by default, and why might a template app log nothing?
answer
- services plus middleware, both required
- properties and headers, no bodies
- query string and Duration are opt-in
- Information under the middleware's own category
- template: Microsoft.AspNetCore = Warning
basics
~10 sCall builder.Services.AddHttpLogging() and app.UseHttpLogging(). By default it logs request method, path, protocol, scheme and headers, plus status code and response headers, at Information; the template's Microsoft.AspNetCore = Warning filters all of it out.
solid answer
~30 sHTTP logging needs both halves: `builder.Services.AddHttpLogging(o => ...)` registers `HttpLoggingOptions` and the services, and `app.UseHttpLogging()` adds `HttpLoggingMiddleware`; calling `UseHttpLogging` without `AddHttpLogging` throws an `InvalidOperationException` while the pipeline is built. `LoggingFields` defaults to `RequestPropertiesAndHeaders | ResponsePropertiesAndHeaders`: method, path, protocol, scheme and request headers, then status code and response headers. The query string, both bodies, trailers and `Duration` are off until you add them. The middleware writes through `ILogger<HttpLoggingMiddleware>` at `Information` and checks `IsEnabled(LogLevel.Information)` first. A `dotnet new webapi` app sets `Microsoft.AspNetCore` to `Warning`, so the middleware passes every request straight through until `Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware` is set to `Information`.
code
json · 9 lines{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
}
}go deeper
Recall the two calls, AddHttpLogging and UseHttpLogging, and that the logs only appear once the middleware's category allows Information.
Explain the default HttpLoggingFields, which useful fields are opt-in, the event ids the middleware writes, and why a template app is silent.
Show how you budget log volume and privacy on a busy gateway, using the category level as an off switch and enabling only the fields an investigation needs.
Decide whether request logs belong in application logs at all or in an edge proxy, and who signs off on fields that may carry personal data.
## What the HTTP logging middleware is `HttpLoggingMiddleware` (namespace `Microsoft.AspNetCore.HttpLogging`, part of the shared framework) records each request and response passing through it as log entries: the request line, headers, optionally bodies, the status code, response headers and the time taken. It writes through the normal logging system, so the entries reach whatever providers the app has: the console, a JSON stream, an OpenTelemetry exporter. In a health-insurance claims gateway it is the quickest way to see what partners really send, without adding logging code to every endpoint. ## Turning it on ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpLogging(o => { o.LoggingFields = HttpLoggingFields.RequestPropertiesAndHeaders | HttpLoggingFields.ResponseStatusCode | HttpLoggingFields.Duration; }); var app = builder.Build(); app.UseHttpLogging(); app.MapPost("/claims", (ClaimSubmission claim) => TypedResults.Accepted($"/claims/{claim.Id}")); app.Run(); ``` Both calls are required: - `AddHttpLogging()` (optionally with an `Action<HttpLoggingOptions>`) registers the options, an object pool for per-request contexts and a `TimeProvider`. - `UseHttpLogging()` adds the middleware. It first verifies the services exist and throws `InvalidOperationException` ("Unable to find the required services. Please add all the required services by calling ...") when `AddHttpLogging` was not called. Only requests that reach the middleware are logged, so it normally goes early in the pipeline; exactly where is a pipeline-ordering decision. ## What it logs by default `HttpLoggingOptions.LoggingFields` is a flags enum, `HttpLoggingFields`, defaulting to `RequestPropertiesAndHeaders | ResponsePropertiesAndHeaders`: | Flag | In the default? | What it adds | |---|---|---| | `RequestPath`, `RequestMethod`, `RequestProtocol`, `RequestScheme` | yes (`RequestProperties`) | path base and path, verb, HTTP version, scheme | | `RequestHeaders` | yes | headers, values shown only for allowlisted names | | `ResponseStatusCode`, `ResponseHeaders` | yes (`ResponsePropertiesAndHeaders`) | status and allowlisted response headers | | `RequestQuery` | **no** | the query string | | `RequestBody`, `ResponseBody` | **no** | bodies, up to a size limit | | `RequestTrailers`, `ResponseTrailers` | **no** | HTTP trailers | | `Duration` | **no** | elapsed time for the whole request | `HttpLoggingFields.All` combines the request and response properties, headers and bodies with `Duration`; even `All` leaves out `RequestQuery` and both trailer flags. Leaving the query string out is deliberate: query strings often carry identifiers and tokens. ## Where the entries go The middleware logs through `ILogger<HttpLoggingMiddleware>`, so its category is `Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware`, and every entry is written at `Information` with a fixed event id: 1. `RequestLog` (1) before the endpoint runs; 2. `ResponseLog` (2) when the response headers are written; 3. `RequestBody` (3) and `ResponseBody` (4) when bodies are enabled; 4. `Duration` (8) at the end; 5. or a single `RequestResponseLog` (9) when `CombineLogs` is on. ## Why a template app shows nothing The very first thing the middleware does for a request is `if (!_logger.IsEnabled(LogLevel.Information)) return _next(context);`. The `webapi` template's `appsettings.json` and `appsettings.Development.json` both set `Microsoft.AspNetCore` to `Warning`, and that prefix covers the middleware's category. The result is a silent no-op: no exception, no entries, almost no cost. The fix is one line in the `Logging:LogLevel` section: ```json "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information" ``` That same check is also the cheapest off switch in production: raising the category to `Warning` disables HTTP logging without touching code. ## What it costs The documentation warns that HTTP logging can reduce performance, especially with bodies, and that it can log personally identifiable information. Every enabled request produces at least two entries with one key/value pair per header, so on a busy gateway the defaults alone can double log volume. Choose fields deliberately, keep bodies off unless you need them, and measure.
- How do you switch HTTP logging off in production without a redeploy?Raise `Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware` to `Warning` (or remove the `Information` override) in configuration. The middleware checks `IsEnabled(LogLevel.Information)` on every request and passes straight through when it is false, so the reloaded level takes effect without touching code.
- Why is the query string not logged by default, and how do you add it?`RequestQuery` is not part of `RequestProperties`, because query strings often carry identifiers and tokens. Add it with `o.LoggingFields |= HttpLoggingFields.RequestQuery` in `AddHttpLogging`, and only after checking that no endpoint puts member or claim identifiers in the query.
- UseHttpLogging works without AddHttpLogging and just uses defaults.
- The default LoggingFields already include request and response bodies.
- HTTP logging writes at Warning, so the template's levels let it through.
- The query string is logged by default as part of the request path.
- HTTP logging writes its own files and ignores Logging:LogLevel.