dotnet / dotnet/aspnetcore

W3CLogger Response Headers

Open
#42,208 3 comments 0 reactions 0 assignees View on GitHub
area-middleware feature-http-logging
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

## Background and Motivation

Implement response headers logging, as a follow up to #41698

Log response headers after all additional request headers have been logged, each set sorted in alphabetical order.

W3CLogger supports logging many pieces of the processed requests, including some HTTP headers. We'd like the ability to pass in a list of additional response headers to be logged, which will be logged after all of the LoggingFields have been written.

Example:

```
#Fields: date time c-ip cs-username s-computername s-ip s-port cs-method cs-uri-stem cs-uri-query sc-status time-taken cs-version cs-host cs(User-Agent) cs(Cookie) cs(Referer) cs(x-client-ssl-protocol) cs(x-forwarded-for) sc(location)
2022-06-15 16:03:54 ::1 - MYPC ::1 5000 GET / - 302 20.9692 HTTP/1.1 localhost:5000 Mozilla/5.0+(Macintosh;+Intel+Mac+OS+X+10_15_7)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/102.0.0.0+Safari/537.36 - - - - /go-here
```

## Proposed API

```
Microsoft.AspNetCore.HttpLogging.W3CLoggerOptions.AdditionalResponseHeaders.get -> System.Collections.Generic.ISet!
```

```diff
namespace Microsoft.AspNetCore.HttpLogging;

public class W3CLoggerOptions
{
+ public ISet AdditionalResponseHeaders { get; }
}
```

```csharp
///
/// List of additional response header values to log.
///


/// Response headers can contain authentication tokens,
/// or private information which may have regulatory concerns
/// under GDPR and other laws. Arbitrary response headers
/// should not be logged unless logs are secure and
/// access controlled and the privacy impact assessed.
///


///
public ISet AdditionalResponseHeaders { get; } = new SortedSet(StringComparer.OrdinalIgnoreCase);
```

## Usage Examples

```csharp
services.AddW3CLogging(logging =>
{
// Log all W3C fields
logging.LoggingFields = W3CLoggingFields.All;
logging.AdditionalRequestHeaders.Clear();
logging.AdditionalRequestHeaders.Add("x-forwarded-for");
logging.AdditionalRequestHeaders.Add("x-client-ssl-protocol");

logging.AdditionalResponseHeaders.Clear();
logging.AdditionalResponseHeaders.Add("location");
});
```

## Alternative Designs

A few were debated on in #41698, this proposal aligns with the approach decided on there.

## Risks

Cookies or credentials can be returned in response headers, similar to request headers.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.