diff --git a/src/Hosting/Hosting/src/Internal/HostingRequestFinishedLog.cs b/src/Hosting/Hosting/src/Internal/HostingRequestFinishedLog.cs index 63fd5f0921ea..e29eca403be9 100644 --- a/src/Hosting/Hosting/src/Internal/HostingRequestFinishedLog.cs +++ b/src/Hosting/Hosting/src/Internal/HostingRequestFinishedLog.cs @@ -13,7 +13,8 @@ internal class HostingRequestFinishedLog : IReadOnlyList Callback = (state, exception) => ((HostingRequestFinishedLog)state).ToString(); - private readonly HttpContext _httpContext; + private readonly string _contentType; + private readonly int _statusCode; private readonly TimeSpan _elapsed; private string _cachedToString; @@ -29,9 +30,9 @@ public KeyValuePair this[int index] case 0: return new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds); case 1: - return new KeyValuePair("StatusCode", _httpContext.Response.StatusCode); + return new KeyValuePair("StatusCode", _statusCode); case 2: - return new KeyValuePair("ContentType", _httpContext.Response.ContentType); + return new KeyValuePair("ContentType", _contentType); default: throw new IndexOutOfRangeException(nameof(index)); } @@ -40,7 +41,8 @@ public KeyValuePair this[int index] public HostingRequestFinishedLog(HttpContext httpContext, TimeSpan elapsed) { - _httpContext = httpContext; + _contentType = httpContext.Response.ContentType; + _statusCode = httpContext.Response.StatusCode; _elapsed = elapsed; } @@ -52,8 +54,8 @@ public override string ToString() CultureInfo.InvariantCulture, "Request finished in {0}ms {1} {2}", _elapsed.TotalMilliseconds, - _httpContext.Response.StatusCode, - _httpContext.Response.ContentType); + _statusCode, + _contentType); } return _cachedToString; diff --git a/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs b/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs index 279fa06aed04..2d77217e3ce0 100644 --- a/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs +++ b/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs @@ -13,7 +13,15 @@ internal class HostingRequestStartingLog : IReadOnlyList Callback = (state, exception) => ((HostingRequestStartingLog)state).ToString(); - private readonly HttpRequest _request; + public string Protocol { get; } + public string Method { get; } + public string ContentType { get; } + public long? ContentLength { get; } + public string Scheme { get; } + public string Host { get; } + public string PathBase { get; } + public string Path { get; } + public string QueryString { get; } private string _cachedToString; @@ -26,23 +34,23 @@ public KeyValuePair this[int index] switch (index) { case 0: - return new KeyValuePair("Protocol", _request.Protocol); + return new KeyValuePair("Protocol", Protocol); case 1: - return new KeyValuePair("Method", _request.Method); + return new KeyValuePair("Method", Method); case 2: - return new KeyValuePair("ContentType", _request.ContentType); + return new KeyValuePair("ContentType", ContentType); case 3: - return new KeyValuePair("ContentLength", _request.ContentLength); + return new KeyValuePair("ContentLength", ContentLength); case 4: - return new KeyValuePair("Scheme", _request.Scheme); + return new KeyValuePair("Scheme", Scheme); case 5: - return new KeyValuePair("Host", _request.Host.ToString()); + return new KeyValuePair("Host", Host); case 6: - return new KeyValuePair("PathBase", _request.PathBase.ToString()); + return new KeyValuePair("PathBase", PathBase); case 7: - return new KeyValuePair("Path", _request.Path.ToString()); + return new KeyValuePair("Path", Path); case 8: - return new KeyValuePair("QueryString", _request.QueryString.ToString()); + return new KeyValuePair("QueryString", QueryString); default: throw new IndexOutOfRangeException(nameof(index)); } @@ -51,7 +59,17 @@ public KeyValuePair this[int index] public HostingRequestStartingLog(HttpContext httpContext) { - _request = httpContext.Request; + var request = httpContext.Request; + + Protocol = request.Protocol; + Method = request.Method; + ContentType = !string.IsNullOrEmpty(request.ContentType) ? Uri.EscapeUriString(request.ContentType) : string.Empty; + ContentLength = request.ContentLength; + Scheme = request.Scheme; + Host = request.Host.Value; + PathBase = request.PathBase.Value; + Path = request.Path.Value; + QueryString = request.QueryString.Value; } public override string ToString() @@ -61,15 +79,15 @@ public override string ToString() _cachedToString = string.Format( CultureInfo.InvariantCulture, "Request starting {0} {1} {2}://{3}{4}{5}{6} {7} {8}", - _request.Protocol, - _request.Method, - _request.Scheme, - _request.Host.Value, - _request.PathBase.Value, - _request.Path.Value, - _request.QueryString.Value, - _request.ContentType, - _request.ContentLength); + Protocol, + Method, + Scheme, + Host, + PathBase, + Path, + QueryString, + ContentType, + ContentLength); } return _cachedToString; diff --git a/src/Hosting/Hosting/test/HostingApplicationTests.cs b/src/Hosting/Hosting/test/HostingApplicationTests.cs index fd8680100de8..a0b275db5ee0 100644 --- a/src/Hosting/Hosting/test/HostingApplicationTests.cs +++ b/src/Hosting/Hosting/test/HostingApplicationTests.cs @@ -428,6 +428,7 @@ private static HostingApplication CreateApplication(out FeatureCollection featur features = new FeatureCollection(); features.Set(new HttpRequestFeature()); + features.Set(new HttpResponseFeature()); var context = new DefaultHttpContext(features); configure?.Invoke(context); httpContextFactory.Setup(s => s.Create(It.IsAny())).Returns(context);