-
Notifications
You must be signed in to change notification settings - Fork 209
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
Adding source context for the logging in RequestLoggingMiddleware. #124
Adding source context for the logging in RequestLoggingMiddleware. #124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Much appreciated 👍
Just realised that there's a decent opportunity to avoid an allocation here, what do you think?
Cheers!
@@ -70,9 +70,10 @@ public async Task Invoke(HttpContext httpContext) | |||
|
|||
bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector, int statusCode, double elapsedMs, Exception ex) | |||
{ | |||
var logger = Log.Logger.ForContext<RequestLoggingMiddleware>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause some additional allocations on every request; alternatively, we could create a:
static readonly LogEventProperty SourceContextProperty = new LogEventProperty("SourceContext", typeof(SerilogRequestLoggingMiddleware).FullName);
and then include this in the list of properties that are manually added to the event a few lines below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But do we not need to have set the source context for the IsEnabled check?
if (!logger.IsEnabled(level)) return false;
if not then I agree with you and I'll change it to your suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right you are, nicely spotted 😅
Would still be nice to trim some allocations down the track, but I think the change as written is good for now 👍
Very minor nitpick:
var logger = Log.Logger.ForContext<RequestLoggingMiddleware>();
It's usually idiomatically written as:
var logger = Log.ForContext<RequestLoggingMiddleware>();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I've fixed the nitpick-issue 👍
c70574c
to
11bdd7e
Compare
👍 great, thanks Adam! |
Fixing Issue #123