Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This long-talked-about feature adds middleware for request logging, based on the popular sample in this blog post.
In comparison with the default request logging performed by the framework, this implementation condenses as much information as possible into a single event, resulting in easier correlation and lower log volumes.
An example completion event might look like:
(I'm using the
{Properties:j}
output formatting option to include properties not present in the log message itself. This makes the events a bit noisier, but it's easier to see what's going on.)Additional properties can be added to the completion event using
IDiagnosticContext
; e.g. in the InlineInitializationSample project'sHomeController.Index()
:The
_diagnosticContext.Add()
call will attachIndexCallCount
to completion events, e.g.:By doing this, it's no longer necessary to speculatively include a lot of information in the request completion event: users who want more details (e.g. form fields from the request, headers, etc.), can write middleware that depends on
IDiagnosticContext
and simply add these themselves.Open to all opinions, including on:
123.456ms
vs123.456 ms
; in a cluttered log event I find the former a bit harder to quickly scan so I've gone with the latter.The PR also replicates some changes, and depends on others, in serilog/serilog-extensions-hosting#9. In particular,
UseSerilog()
will now register theIDiagnosticContext
andDiagnosticContext
types, as well asILogger
if available, and the middleware consumesDiagnosticContext
. CC @andrewlock.