-
Notifications
You must be signed in to change notification settings - Fork 308
Conversation
|
||
public HostingRequestScope(HttpContext httpContext) | ||
{ | ||
this._httpContext = httpContext; |
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.
nit: remove this.
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.
🆗
2796353
to
7921894
Compare
{ | ||
if (logger.IsEnabled(LogLevel.Information)) | ||
{ | ||
logger.Log(LogLevel.Information, 1, new HostingRequestStarting(httpContext), null, HostingRequestStarting.Callback); |
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.
null and 1 should use the optional args syntax.
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 ci broke |
See also aspnet/Logging#261 |
{ | ||
private readonly HttpContext _httpContext; | ||
|
||
private FeatureReference<IHttpRequestIdentifierFeature> _requestIdentifierFeatureReference; |
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.
With aspnet/HttpAbstractions#440 merged, remove
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.
Nice! Hmm... Why don't I see it yet? I'm using 1.0.0-rc1-15826
... Must still be working its way through the system
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.
Will merge back into other PR, and add comment changes - hopefully it will flow though soon :)
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.
Not sure, updated #409 with all the changes but isn't picking up TraceIdentifier
on HttpContext
yet :(
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.
Coherence hasn't passed, but you can reference https://www.myget.org/F/aspnetvolatiledev/api/v2 instead
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.
Was test break, applied fix; build is now happy against aspnetvolatiledev for other PR
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.
Is working now! 💃
PR #409 is working now; which you'll need to merge as the HttpAbstraction changes look like they have flowed through; so currently this PR will break the CI. Added all my comment changes from this to it - as they were around the upcoming break. |
Sorry, was wrong, don't think this PR will break the CI as it stands (without the other changes) |
} | ||
} | ||
|
||
private class HostingRequestScope : ILogValues |
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.
HostingLogScope. RequestScope sounds like DI
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.
Okay, but the other two are still HostingRequestXxx
Rebased #409 onto last commit and picked up remaining clean up and ci break |
|
||
private class HostingLogScope : ILogValues | ||
{ | ||
private readonly HttpContext _httpContext; |
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.
Could move this up to HttpAbstractions
implement in safe general way (but not alloc free), and thus drop the HostingLogScope
class create in RequestScope(...)
public abstract class HttpContext : ILogValues
{
...
public override string ToString() => $"RequestId:{TraceIdentifier} RequestPath:{Request.Path}";
IEnumerable<KeyValuePair<string, object>> ILogValues.GetValues()
{
yield return new KeyValuePair<string, object>("RequestId", TraceIdentifier);
yield return new KeyValuePair<string, object>("RequestPath", Request.Path.ToString());
}
}
Specific derived classes could then override with caching as per HostingLogScope
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.
I wouldn't recommend it. Hosting has an opinion on what is appropriate to log at this moment in the request's execution. That opinion is not valid or useful elsewhere in the app.
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.
Hmm, then switch
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
{
return logger.BeginScopeImpl(new HostingLogScope(httpContext));
}
for
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
{
return logger.IsEnabled(LogLevel.Critical)
? logger.BeginScopeImpl(new HostingLogScope(httpContext))
: null;
}
Would that work? Else might regress on perf.
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.
Per discussion with lou, that's not really correct. Scopes are relevant to all loggers, not just to the current one. We'd need a sort of global 'all loggers disabled' check.
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.
Doesn't it go via Microsoft.Extensions.Logging.Logger
which enumerates all loggers in the IsEnabled
method bailing on first true?
Also have fast path PR for no loggers for the check: https://github.com/aspnet/Logging/pull/254/files
An IsEnabled
with no params would be prettier, but would change the interface.
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, you mean because its an extension method so can be used in multiple places rather than just on the top level logger?
Or adding a logger at a particular point in the pipeline, to only log specific bits?
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.
It turns out it was a bug to check the IsEnabled to short-circuit the scope. Even if messages in the logger
's category are entirely disabled, there can be other messages in the application that will be logged and will need the logger scope available... We might be able to optimize the BeginScopeImpl a little bit more and get the per-request overhead down to a single object with one field, then measure it from there to see how much impact that is having
d2feb06
to
3845e33
Compare
@Tratcher updated |
throw; | ||
finally | ||
{ | ||
logger.RequestFinished(httpContext); |
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.
It's still weird that this fires for unhandled exceptions and logs 200 OK.
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.
How would you feel about just logging the elapsed time on failure then - no contenttype/status code. It really seems like the best we can do for now
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.
Better, but you don't have the elapsed time here, do you? I would expect logger.RequestFailed()
, with or without the exception.
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.
An alternative is to hook the request completed event to have the correct status code and possibly more accurate overall time. That would need to be weighed against the overhead of the event hook per request - but it can be done only if that level is enabled - so it's no cost at all when no loggers aren't added.
3845e33
to
561b10b
Compare
@Tratcher updated |
#409 changes:
Assert.NotNull(httpContext);
Assert.IsType<FastHttpRequestIdentifierFeature>(httpContext.Features.Get<IHttpRequestIdentifierFeature>()); to Assert.NotNull(httpContext);
Assert.IsType<HttpRequestIdentifierFeature>(httpContext.Features.Get<IHttpRequestIdentifierFeature>());
Assert.False(string.IsNullOrWhiteSpace(httpContext.TraceIdentifier));
Assert.Same(httpContext.TraceIdentifier, httpContext.Features.Get<IHttpRequestIdentifierFeature>().TraceIdentifier); |
@@ -93,27 +93,40 @@ public virtual IApplication Start() | |||
{ | |||
var httpContext = contextFactory.CreateHttpContext(features); | |||
httpContext.ApplicationServices = _applicationServices; | |||
|
|||
var requestIdentifier = GetRequestIdentifier(httpContext); |
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.
Remove this line.
|
* TraceIdentifier is done at the last moment, or not at all * Request starting and finished messages are added * This pair provide many of the top-level values that you would have found in server log files.
5085cce
to
8cdff72
Compare
Will wait until CI for hosting is green to push. Currently broken due to issues not related to this change. |
found in server log files.