Skip to content
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

Conversation

adamlith
Copy link
Contributor

Fixing Issue #123

Copy link
Member

@nblumhardt nblumhardt left a 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>();
Copy link
Member

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?

Copy link
Contributor Author

@adamlith adamlith Aug 30, 2019

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.

Copy link
Member

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>();

Copy link
Contributor Author

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 👍

@adamlith adamlith force-pushed the feature/source-context-in-request-logging-middleware branch from c70574c to 11bdd7e Compare September 3, 2019 08:44
@nblumhardt
Copy link
Member

👍 great, thanks Adam!

@nblumhardt nblumhardt merged commit 64924c4 into serilog:dev Sep 3, 2019
@nblumhardt nblumhardt mentioned this pull request Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants