-
Notifications
You must be signed in to change notification settings - Fork 209
Add RequestHost & RequestScheme to log properties #146
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
Conversation
Fixed, thanks @sungam3r |
Thanks @tomkerkhove! These are useful, and there will be a lot of users who'd benefit from them, but others may not (and/or may need other information about the request). Perhaps, to prevent the request completion events from getting bloated, we allow customization of the set of logged properties through the E.g.: app.UseSerilogRequestLogging(opts => {
opts.ExtendDiagnosticContext = (diagnosticContext, httpContext) => {
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
};
}); (Not sure about the names/details, but the two delegate parameters would be an Thoughts? |
Sounds good @nblumhardt. What about before & after ‘next’ middleware has been executed? Would only “after” be enough? What about when an exception happens? |
I think after would be sufficient for the cases I can think of; it's not all that common to remove information from the request/context as it executes, but information is sometimes added. Immediately before retrieving the context values in
If an exception occurred, we'd have a chance to pass it through, too (as we would the timing info). Do we try to make the argument list a bit broader than just |
What if, instead of using the following: app.UseSerilogRequestLogging(opts => {
opts.ExtendDiagnosticContext = (diagnosticContext, httpContext) => {
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
};
}); You can just provide a func that returns a dictionary and input is app.UseSerilogRequestLogging(options => {
options.GatherInformationBeforeCall = (httpContext) => {
return new Dictionary<string, string>{{"RequestHost", httpContext.Request.Host.Value}};
};
options.GatherInformationAfter = (httpContext) => {
return new Dictionary<string, string>{{"RequestHost", httpContext.Request.Host.Value}};
};
}); Not required, otherwise I'll have a look at the suggested scenario. |
Thanks for the suggestion, @tomkerkhove! There are a couple of complications with using dictionaries here that I think we should avoid; first, they're rather large to allocate, but also, The before/after call split is tricky, I'd lean towards keeping it to a single callback to minimize the (ever-growing :-)) surface area, unless we've got a compelling use case for a before hook. Semi-related, would it be misleading to call it |
Signed-off-by: Tom Kerkhove <[email protected]>
…ve/serilog-aspnetcore into request-log-properties
I've aligned with your suggested but I'm not an expert on Was going to update the tests but they are still TODO 😅 |
Fantastic - thanks Tom! Just made some nitpicky edits to the README and examples, but this looks good, let's ship it :-) |
My pleasure! A few questions though:
|
Cool! :-) Yes, the diagnostic context will add properties just like that 👍 Some testing on the The files in a PR have an 'edit' button that will simplify editing someone's fork in a PR (they need to have left the "Allow edits from maintainers" checkbox ticked when creating the PR). It's also possible to push directly to the PR source branch in their repo via the same permissions, using regular I use it quite a bit for nitpicky stuff that generates busywork - it can be ideal for avoiding trivial review cycles. Hope you find it handy! :-) |
Awesome, thanks! Have a lot of small comments as well and this way I can help them, thanks for the tip! |
@nblumhardt Just for the record - the one is ForwardedHeadersMiddleware that rewrites headers collection. There is no way to enrich with original headers no matter where |
Add RequestHost & RequestScheme to log properties and list all options in docs