Skip to content

Add RequestHost & RequestScheme to log properties #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Then, in your application's _Startup.cs_, add the middleware with `UseSerilogReq
// Other app configuration
```

It's important that the `UseSerilogRequestLogging()` call appears _before_ handlers such as MVC. The middleware will not time or log components that appear before it in the pipeline. (This can be utilized to exclude noisy handlers from logging, such as `UseStaticFiles()`, by placing `UseSerilogRequestLogging()` after them.)
It's important that the `UseSerilogRequestLogging()` call appears _before_ handlers such as MVC. The middleware will not time or log components that appear before it in the pipeline. You can override the message template by specifying `messageTemplate`. (This can be utilized to exclude noisy handlers from logging, such as `UseStaticFiles()`, by placing `UseSerilogRequestLogging()` after them.)

During request processing, additional properties can be attached to the completion event using `IDiagnosticContext.Set()`:

Expand All @@ -159,6 +159,15 @@ During request processing, additional properties can be attached to the completi

This pattern has the advantage of reducing the number of log events that need to be constructed, transmitted, and stored per HTTP request. Having many properties on the same event can also make correlation of request details and other data easier.

The following request information will be added as log properties:

* `RequestMethod`
* `RequestScheme`
* `RequestHost`
* `RequestPath`
* `StatusCode`
* `Elapsed`

### Inline initialization

You can alternatively configure Serilog inline, in `BuildWebHost()`, using a delegate as shown below:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector

if (!collector.TryComplete(out var collectedProperties))
collectedProperties = NoProperties;

// Last-in (correctly) wins...
var properties = collectedProperties.Concat(new[]
{
new LogEventProperty("RequestMethod", new ScalarValue(httpContext.Request.Method)),
new LogEventProperty("RequestHost", new ScalarValue(httpContext.Request.Host.Value)),
new LogEventProperty("RequestScheme", new ScalarValue(httpContext.Request.Scheme)),
new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext))),
new LogEventProperty("StatusCode", new ScalarValue(statusCode)),
new LogEventProperty("Elapsed", new ScalarValue(elapsedMs))
Expand Down