From 04b365a22f130af93298aa5388e2fba441c44dca Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 27 Aug 2018 11:59:56 -0700 Subject: [PATCH] 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. --- .../Internal/HostingLoggerExtensions.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs index a0579880a0..ce333f232c 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs @@ -94,7 +94,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal private class HostingLogScope : IReadOnlyList> { - private readonly HttpContext _httpContext; + private readonly string _path; + private readonly string _traceIdentifier; private readonly string _correlationId; private string _cachedToString; @@ -113,11 +114,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal { if (index == 0) { - return new KeyValuePair("RequestId", _httpContext.TraceIdentifier); + return new KeyValuePair("RequestId", _traceIdentifier); } else if (index == 1) { - return new KeyValuePair("RequestPath", _httpContext.Request.Path.ToString()); + return new KeyValuePair("RequestPath", _path); } else if (index == 2) { @@ -130,7 +131,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal public HostingLogScope(HttpContext httpContext, string correlationId) { - _httpContext = httpContext; + _traceIdentifier = httpContext.TraceIdentifier; + _path = httpContext.Request.Path.ToString(); _correlationId = correlationId; } @@ -141,8 +143,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal _cachedToString = string.Format( CultureInfo.InvariantCulture, "RequestId:{0} RequestPath:{1}", - _httpContext.TraceIdentifier, - _httpContext.Request.Path); + _traceIdentifier, + _path); } return _cachedToString;