From 2bb2f955327ea52c4b0536732d85b4a42f16e1a5 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Wed, 28 Oct 2015 12:10:07 -0700 Subject: [PATCH] Log messages from HostingEngine are sometimes null #454 --- .../Internal/HostingLoggerExtensions.cs | 159 +++++++++++------- 1 file changed, 99 insertions(+), 60 deletions(-) diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs index 0d79f7ae63..a92eac1444 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Threading; using Microsoft.AspNet.Http; using Microsoft.Extensions.Logging; @@ -108,55 +107,75 @@ namespace Microsoft.AspNet.Hosting.Internal _httpContext = httpContext; } - public override string ToString() => _cachedToString ?? Interlocked.CompareExchange( - ref _cachedToString, - $"RequestId:{_httpContext.TraceIdentifier} RequestPath:{_httpContext.Request.Path}", - null); - - public IEnumerable> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange( - ref _cachedGetValues, - new[] + public override string ToString() + { + if (_cachedToString == null) { - new KeyValuePair("RequestId", _httpContext.TraceIdentifier), - new KeyValuePair("RequestPath", _httpContext.Request.Path.ToString()), - }, - null); + _cachedToString = $"RequestId:{_httpContext.TraceIdentifier} RequestPath:{_httpContext.Request.Path}"; + } + + return _cachedToString; + } + + public IEnumerable> GetValues() + { + if (_cachedGetValues == null) + { + _cachedGetValues = new[] + { + new KeyValuePair("RequestId", _httpContext.TraceIdentifier), + new KeyValuePair("RequestPath", _httpContext.Request.Path.ToString()), + }; + } + + return _cachedGetValues; + } } private class HostingRequestStarting : ILogValues { internal static readonly Func Callback = (state, exception) => ((HostingRequestStarting)state).ToString(); - private readonly HttpContext _httpContext; + private readonly HttpRequest _request; private string _cachedToString; private IEnumerable> _cachedGetValues; public HostingRequestStarting(HttpContext httpContext) { - _httpContext = httpContext; + _request = httpContext.Request; } - public override string ToString() => _cachedToString ?? Interlocked.CompareExchange( - ref _cachedToString, - $"Request starting {_httpContext.Request.Protocol} {_httpContext.Request.Method} {_httpContext.Request.Scheme}://{_httpContext.Request.Host}{_httpContext.Request.PathBase}{_httpContext.Request.Path}{_httpContext.Request.QueryString} {_httpContext.Request.ContentType} {_httpContext.Request.ContentLength}", - null); - - public IEnumerable> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange( - ref _cachedGetValues, - new[] + public override string ToString() + { + if (_cachedToString == null) { - new KeyValuePair("Protocol", _httpContext.Request.Protocol), - new KeyValuePair("Method", _httpContext.Request.Method), - new KeyValuePair("ContentType", _httpContext.Request.ContentType), - new KeyValuePair("ContentLength", _httpContext.Request.ContentLength), - new KeyValuePair("Scheme", _httpContext.Request.Scheme.ToString()), - new KeyValuePair("Host", _httpContext.Request.Host.ToString()), - new KeyValuePair("PathBase", _httpContext.Request.PathBase.ToString()), - new KeyValuePair("Path", _httpContext.Request.Path.ToString()), - new KeyValuePair("QueryString", _httpContext.Request.QueryString.ToString()), - }, - null); + _cachedToString = $"Request starting {_request.Protocol} {_request.Method} {_request.Scheme}://{_request.Host}{_request.PathBase}{_request.Path}{_request.QueryString} {_request.ContentType} {_request.ContentLength}"; + } + + return _cachedToString; + } + + public IEnumerable> GetValues() + { + if (_cachedGetValues == null) + { + _cachedGetValues = new[] + { + new KeyValuePair("Protocol", _request.Protocol), + new KeyValuePair("Method", _request.Method), + new KeyValuePair("ContentType", _request.ContentType), + new KeyValuePair("ContentLength", _request.ContentLength), + new KeyValuePair("Scheme", _request.Scheme.ToString()), + new KeyValuePair("Host", _request.Host.ToString()), + new KeyValuePair("PathBase", _request.PathBase.ToString()), + new KeyValuePair("Path", _request.Path.ToString()), + new KeyValuePair("QueryString", _request.QueryString.ToString()), + }; + } + + return _cachedGetValues; + } } private class HostingRequestFinished @@ -175,20 +194,30 @@ namespace Microsoft.AspNet.Hosting.Internal _elapsed = elapsed; } - public override string ToString() => _cachedToString ?? Interlocked.CompareExchange( - ref _cachedToString, - $"Request finished in {_elapsed.TotalMilliseconds}ms {_httpContext.Response.StatusCode} {_httpContext.Response.ContentType}", - null); - - public IEnumerable> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange( - ref _cachedGetValues, - new[] + public override string ToString() + { + if (_cachedToString == null) { - new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds), - new KeyValuePair("StatusCode", _httpContext.Response.StatusCode), - new KeyValuePair("ContentType", _httpContext.Response.ContentType), - }, - null); + _cachedToString = $"Request finished in {_elapsed.TotalMilliseconds}ms {_httpContext.Response.StatusCode} {_httpContext.Response.ContentType}"; + } + + return _cachedToString; + } + + public IEnumerable> GetValues() + { + if (_cachedGetValues == null) + { + _cachedGetValues = new[] + { + new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds), + new KeyValuePair("StatusCode", _httpContext.Response.StatusCode), + new KeyValuePair("ContentType", _httpContext.Response.ContentType), + }; + } + + return _cachedGetValues; + } } private class HostingRequestFailed @@ -207,20 +236,30 @@ namespace Microsoft.AspNet.Hosting.Internal _elapsed = elapsed; } - public override string ToString() => _cachedToString ?? Interlocked.CompareExchange( - ref _cachedToString, - $"Request finished in {_elapsed.TotalMilliseconds}ms 500", - null); - - public IEnumerable> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange( - ref _cachedGetValues, - new[] + public override string ToString() + { + if (_cachedToString == null) { - new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds), - new KeyValuePair("StatusCode", 500), - new KeyValuePair("ContentType", null), - }, - null); + _cachedToString = $"Request finished in {_elapsed.TotalMilliseconds}ms 500"; + } + + return _cachedToString; + } + + public IEnumerable> GetValues() + { + if (_cachedGetValues == null) + { + _cachedGetValues = new[] + { + new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds), + new KeyValuePair("StatusCode", 500), + new KeyValuePair("ContentType", null), + }; + } + + return _cachedGetValues; + } } } }