skip to content

In ASP.NET Core, how does AddW3CLogging differ from AddHttpLogging, and which W3CLoggerOptions defaults decide where its files go and how long they are kept?

level: middleimportance: nice to knowfreq 16%

answer

  1. files, not ILogger entries
  2. W3C extended log format, like IIS
  3. {ContentRoot}/logs, w3clog- prefix
  4. 10 MB per file, 4 files kept
  5. query string and client IP on by default

basics

~10 s

AddW3CLogging plus UseW3CLogging writes one W3C extended-format line per request into text files, bypassing ILogger. By default files go to {ContentRoot}/logs as w3clog-<date>.<n>.txt, roll at 10 MB, and only the newest 4 are kept.

solid answer

~40 s

The W3C logger is a separate feature: `services.AddW3CLogging(o => ...)` (it takes a configure delegate) and `app.UseW3CLogging()`. It writes fixed-column lines in the W3C extended log file format, the format IIS uses, straight to files through its own background writer, so `Logging:LogLevel`, providers and JSON formatting play no part. `W3CLoggerOptions` defaults: `LogDirectory` empty, meaning `{ContentRoot}/logs`; `FileName` `"w3clog-"`, giving `w3clog-20261006.0000.txt`; `FileSizeLimit` 10 MB before rolling to the next number; `RetainedFileCountLimit` 4, after which older files are deleted; `FlushInterval` 1 second. `LoggingFields` defaults to everything except `UserName` and `Cookie`, so the query string and client IP are recorded. Use `AdditionalRequestHeaders` for extra headers. There are no bodies and no per-endpoint settings; that is what the HTTP logging middleware is for.

code

bash · 3 lines
bash
ls ./logs
# w3clog-20261006.0000.txt  w3clog-20261006.0001.txt
head -n 4 ./logs/w3clog-20261006.0001.txt

go deeper

for a junior

Recall that the W3C logger writes request lines to text files and needs AddW3CLogging and UseW3CLogging.

for a middle

Explain how it differs from HTTP logging and recite the W3CLoggerOptions defaults: logs folder, w3clog- prefix, 10 MB, 4 files, 1 second flush.

for a senior

Show the privacy and operations traps: the query string and client IP on by default, short retention, blocking when the queue fills, and lost files in containers.

for a principal

Decide whether file-based access logs belong in the app at all or at the proxy or platform layer, and who owns their retention.

## Two different features ASP.NET Core ships two request loggers in `Microsoft.AspNetCore.HttpLogging`: | | HTTP logging | W3C logging | |---|---|---| | Registration | `AddHttpLogging` + `UseHttpLogging` | `AddW3CLogging(o => ...)` + `UseW3CLogging` | | Output | `ILogger` entries, any provider | text files in W3C extended log format | | Filtering | `Logging:LogLevel`, providers | none; every request reaching it is written | | Bodies | optional, with limits | never | | Per endpoint | `[HttpLogging]`, `WithHttpLogging`, interceptors | no | | Typical use | structured diagnostics in a log store | access logs for tools that read IIS-style files | Because the W3C logger writes its own files, setting `Logging:LogLevel:Default` to `None` or calling `ClearProviders` does not stop it; only removing `UseW3CLogging` or changing its options does. ## The file format Each file starts with directive lines (`#Version: 1.0`, `#Start-Date: ...`, `#Fields: ...`) followed by one space-separated line per request, with the columns named in `#Fields` in a fixed order. Log analysers that understand IIS logs can read these files without custom parsing. ## Defaults that matter ```csharp builder.Services.AddW3CLogging(o => { o.LoggingFields = W3CLoggingFields.Date | W3CLoggingFields.Time | W3CLoggingFields.Method | W3CLoggingFields.UriStem | W3CLoggingFields.ProtocolStatus | W3CLoggingFields.TimeTaken; o.LogDirectory = "/var/log/claims-gateway"; o.RetainedFileCountLimit = 14; o.AdditionalRequestHeaders.Add("X-Correlation-Id"); }); app.UseW3CLogging(); ``` `W3CLoggerOptions` and its defaults: - `LogDirectory`: empty, which means a `logs` folder under the content root; a relative path is also resolved against the content root. Setting it to an empty string throws. - `FileName`: `w3clog-`, followed by the date and a four-digit sequence, for example `w3clog-20261006.0000.txt`. - `FileSizeLimit`: 10 MB; when a file exceeds it the writer rolls to the next sequence number. - `RetainedFileCountLimit`: 4 (must be between 1 and 10,000); after each write batch, files beyond the newest four are deleted. - `FlushInterval`: 1 second between batches. - `LoggingFields`: `Date`, `Time`, `ServerName`, `Method`, `UriStem`, `UriQuery`, `ProtocolStatus`, `TimeTaken`, `ProtocolVersion`, `Host`, `UserAgent`, `Referer` and the connection fields (`ClientIpAddress`, `ServerIpAddress`, `ServerPort`); `UserName` and `Cookie` are the only fields left out. - `AdditionalRequestHeaders`: a set of extra request header names to log (added in ASP.NET Core 7). With the defaults, the newest four files of at most about 10 MB each are all that survive, so a busy service keeps minutes or hours of history, not days. ## Privacy and operations In a health-insurance claims gateway, the defaults deserve a second look: 1. **`UriQuery` is on.** Unlike the HTTP logging middleware, the W3C logger records the query string by default, so `?memberId=M-48812` lands in a file. 2. **`ClientIpAddress` is on.** Client addresses are personal data in many jurisdictions. 3. **`Cookie` is off**, but adding `W3CLoggingFields.All` turns it and `UserName` on. 4. **Headers are opt-in by name.** `AdditionalRequestHeaders` logs the listed headers' values in full; never add `Authorization` or an API key header. Operationally, the writer queues lines in a bounded collection of 1,024 entries; when the disk writer falls behind, adding to the queue blocks the request thread. In a container, files written inside the container's filesystem disappear with it unless `LogDirectory` points at a mounted volume, which is why containerised apps usually prefer `ILogger` output on standard out.

  • Why does a W3C log on a busy gateway only cover the last hour after a restart?
    The defaults keep at most four files (`RetainedFileCountLimit` = 4) of about 10 MB each (`FileSizeLimit`). After each write batch, older files matching the `w3clog-` prefix are deleted. A high request rate fills 40 MB quickly; raise the limits or ship the files elsewhere.
  • How do you log the X-Forwarded-For header in W3C logs?
    Add it to `W3CLoggerOptions.AdditionalRequestHeaders` inside `AddW3CLogging`. The set exists since ASP.NET Core 7 and logs each listed header's value as an extra column. Header values are written in full, so add only non-secret headers.

  • W3C logs go through ILogger, so Logging:LogLevel can silence them.
  • The W3C logger omits the query string by default, like HTTP logging.
  • W3C files are kept forever unless a cleanup job deletes them.
  • AddW3CLogging can log request bodies with a size limit.
  • Files are written to the process working directory by default.