Skip to content

Commit 398cc94

Browse files
committed
Align with suggested approach
Signed-off-by: Tom Kerkhove <[email protected]>
1 parent 3852fa1 commit 398cc94

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,23 @@ This pattern has the advantage of reducing the number of log events that need to
162162
The following request information will be added as log properties:
163163

164164
* `RequestMethod`
165-
* `RequestScheme`
166-
* `RequestHost`
167165
* `RequestPath`
168166
* `StatusCode`
169167
* `Elapsed`
170168

169+
Not enough? You can extend the information that is being emitted by using `options.EnrichDiagnosticContext`:
170+
171+
```csharp
172+
app.UseSerilogRequestLogging(options =>
173+
{
174+
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
175+
{
176+
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
177+
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
178+
};
179+
});
180+
```
181+
171182
### Inline initialization
172183

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

samples/EarlyInitializationSample/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3030
// Write streamlined request completion events, instead of the more verbose ones from the framework.
3131
// To use the default framework request logging instead, remove this line and set the "Microsoft"
3232
// level in appsettings.json to "Information".
33-
app.UseSerilogRequestLogging();
34-
33+
app.UseSerilogRequestLogging(options =>
34+
{
35+
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
36+
{
37+
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
38+
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
39+
};
40+
});
41+
3542
app.UseRouting();
3643

3744
app.UseEndpoints(endpoints =>

samples/InlineInitializationSample/Startup.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3232
// Write streamlined request completion events, instead of the more verbose ones from the framework.
3333
// To use the default framework request logging instead, remove this line and set the "Microsoft"
3434
// level in appsettings.json to "Information".
35-
app.UseSerilogRequestLogging();
35+
app.UseSerilogRequestLogging(options =>
36+
{
37+
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
38+
{
39+
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
40+
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
41+
};
42+
});
3643

3744
app.UseRouting();
3845

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class RequestLoggingMiddleware
2929
readonly RequestDelegate _next;
3030
readonly DiagnosticContext _diagnosticContext;
3131
readonly MessageTemplate _messageTemplate;
32+
readonly Action<IDiagnosticContext, HttpContext> _enrichDiagnosticContext;
3233
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
3334
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];
3435

@@ -39,6 +40,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
3940
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
4041

4142
_getLevel = options.GetLevel;
43+
_enrichDiagnosticContext = options.EnrichDiagnosticContext;
4244
_messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate);
4345
}
4446

@@ -78,13 +80,14 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
7880

7981
if (!collector.TryComplete(out var collectedProperties))
8082
collectedProperties = NoProperties;
81-
83+
84+
// Enrich diagnostic context
85+
_enrichDiagnosticContext?.Invoke(_diagnosticContext, httpContext);
86+
8287
// Last-in (correctly) wins...
8388
var properties = collectedProperties.Concat(new[]
8489
{
8590
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)),
8891
new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext))),
8992
new LogEventProperty("StatusCode", new ScalarValue(statusCode)),
9093
new LogEventProperty("Elapsed", new ScalarValue(elapsedMs))

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class RequestLoggingOptions
4545
/// </value>
4646
public Func<HttpContext, double, Exception, LogEventLevel> GetLevel { get; set; }
4747

48+
/// <summary>
49+
/// Gets or sets the action that allows you to enrich the diagnostic context
50+
/// </summary>
51+
public Action<IDiagnosticContext, HttpContext> EnrichDiagnosticContext { get; set; }
52+
4853
internal RequestLoggingOptions() { }
4954
}
5055
}

0 commit comments

Comments
 (0)