Skip to content

Commit 3852fa1

Browse files
committed
Add RequestHost & RequestScheme to log properties
1 parent 7116569 commit 3852fa1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Then, in your application's _Startup.cs_, add the middleware with `UseSerilogReq
134134
// Other app configuration
135135
```
136136

137-
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.)
137+
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.)
138138

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

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

160160
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.
161161

162+
The following request information will be added as log properties:
163+
164+
* `RequestMethod`
165+
* `RequestScheme`
166+
* `RequestHost`
167+
* `RequestPath`
168+
* `StatusCode`
169+
* `Elapsed`
170+
162171
### Inline initialization
163172

164173
You can alternatively configure Serilog inline, in `BuildWebHost()`, using a delegate as shown below:

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
7878

7979
if (!collector.TryComplete(out var collectedProperties))
8080
collectedProperties = NoProperties;
81-
81+
8282
// Last-in (correctly) wins...
8383
var properties = collectedProperties.Concat(new[]
8484
{
8585
new LogEventProperty("RequestMethod", new ScalarValue(httpContext.Request.Method)),
86+
new LogEventProperty("RequestHost", new ScalarValue(httpContext.Request.Host.Value)),
87+
new LogEventProperty("RequestScheme", new ScalarValue(httpContext.Request.Scheme)),
8688
new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext))),
8789
new LogEventProperty("StatusCode", new ScalarValue(statusCode)),
8890
new LogEventProperty("Elapsed", new ScalarValue(elapsedMs))

0 commit comments

Comments
 (0)