Don't hold onto the HttpContext in the HostingLogScope (#1531)

- Multiple things capture the ExecutionContext, reduce the changes of improperly rooting the HttpContext when we only need a few properties from the request.
This commit is contained in:
David Fowler 2018-08-27 11:59:56 -07:00 committed by GitHub
parent ef465439af
commit 04b365a22f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -94,7 +94,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
private class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>> private class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
{ {
private readonly HttpContext _httpContext; private readonly string _path;
private readonly string _traceIdentifier;
private readonly string _correlationId; private readonly string _correlationId;
private string _cachedToString; private string _cachedToString;
@ -113,11 +114,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{ {
if (index == 0) if (index == 0)
{ {
return new KeyValuePair<string, object>("RequestId", _httpContext.TraceIdentifier); return new KeyValuePair<string, object>("RequestId", _traceIdentifier);
} }
else if (index == 1) else if (index == 1)
{ {
return new KeyValuePair<string, object>("RequestPath", _httpContext.Request.Path.ToString()); return new KeyValuePair<string, object>("RequestPath", _path);
} }
else if (index == 2) else if (index == 2)
{ {
@ -130,7 +131,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
public HostingLogScope(HttpContext httpContext, string correlationId) public HostingLogScope(HttpContext httpContext, string correlationId)
{ {
_httpContext = httpContext; _traceIdentifier = httpContext.TraceIdentifier;
_path = httpContext.Request.Path.ToString();
_correlationId = correlationId; _correlationId = correlationId;
} }
@ -141,8 +143,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
_cachedToString = string.Format( _cachedToString = string.Format(
CultureInfo.InvariantCulture, CultureInfo.InvariantCulture,
"RequestId:{0} RequestPath:{1}", "RequestId:{0} RequestPath:{1}",
_httpContext.TraceIdentifier, _traceIdentifier,
_httpContext.Request.Path); _path);
} }
return _cachedToString; return _cachedToString;