Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Log messages from HostingEngine are sometimes null #454
Browse files Browse the repository at this point in the history
  • Loading branch information
kichalla committed Oct 28, 2015
1 parent 9be0758 commit 28e064f
Showing 1 changed file with 74 additions and 44 deletions.
118 changes: 74 additions & 44 deletions src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,46 @@ private class HostingRequestStarting : ILogValues
{
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((HostingRequestStarting)state).ToString();

private readonly HttpContext _httpContext;
private readonly HttpRequest _request;

private string _cachedToString;
private IEnumerable<KeyValuePair<string, object>> _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 override string ToString()
{
if (_cachedToString == null)
{
_cachedToString = $"Request starting {_request.Protocol} {_request.Method} {_request.Scheme}://{_request.Host}{_request.PathBase}{_request.Path}{_request.QueryString} {_request.ContentType} {_request.ContentLength}";
}

public IEnumerable<KeyValuePair<string, object>> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange(
ref _cachedGetValues,
new[]
return _cachedToString;
}

public IEnumerable<KeyValuePair<string, object>> GetValues()
{
if (_cachedGetValues == null)
{
new KeyValuePair<string, object>("Protocol", _httpContext.Request.Protocol),
new KeyValuePair<string, object>("Method", _httpContext.Request.Method),
new KeyValuePair<string, object>("ContentType", _httpContext.Request.ContentType),
new KeyValuePair<string, object>("ContentLength", _httpContext.Request.ContentLength),
new KeyValuePair<string, object>("Scheme", _httpContext.Request.Scheme.ToString()),
new KeyValuePair<string, object>("Host", _httpContext.Request.Host.ToString()),
new KeyValuePair<string, object>("PathBase", _httpContext.Request.PathBase.ToString()),
new KeyValuePair<string, object>("Path", _httpContext.Request.Path.ToString()),
new KeyValuePair<string, object>("QueryString", _httpContext.Request.QueryString.ToString()),
},
null);
_cachedGetValues = new[]
{
new KeyValuePair<string, object>("Protocol", _request.Protocol),
new KeyValuePair<string, object>("Method", _request.Method),
new KeyValuePair<string, object>("ContentType", _request.ContentType),
new KeyValuePair<string, object>("ContentLength", _request.ContentLength),
new KeyValuePair<string, object>("Scheme", _request.Scheme.ToString()),
new KeyValuePair<string, object>("Host", _request.Host.ToString()),
new KeyValuePair<string, object>("PathBase", _request.PathBase.ToString()),
new KeyValuePair<string, object>("Path", _request.Path.ToString()),
new KeyValuePair<string, object>("QueryString", _request.QueryString.ToString()),
};
};

return _cachedGetValues;
}
}

private class HostingRequestFinished
Expand All @@ -175,20 +185,30 @@ public HostingRequestFinished(HttpContext httpContext, TimeSpan elapsed)
_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 override string ToString()
{
if (_cachedToString == null)
{
_cachedToString = $"Request finished in {_elapsed.TotalMilliseconds}ms {_httpContext.Response.StatusCode} {_httpContext.Response.ContentType}";
}

public IEnumerable<KeyValuePair<string, object>> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange(
ref _cachedGetValues,
new[]
return _cachedToString;
}

public IEnumerable<KeyValuePair<string, object>> GetValues()
{
if (_cachedGetValues == null)
{
new KeyValuePair<string, object>("ElapsedMilliseconds", _elapsed.TotalMilliseconds),
new KeyValuePair<string, object>("StatusCode", _httpContext.Response.StatusCode),
new KeyValuePair<string, object>("ContentType", _httpContext.Response.ContentType),
},
null);
_cachedGetValues = new[]
{
new KeyValuePair<string, object>("ElapsedMilliseconds", _elapsed.TotalMilliseconds),
new KeyValuePair<string, object>("StatusCode", _httpContext.Response.StatusCode),
new KeyValuePair<string, object>("ContentType", _httpContext.Response.ContentType),
};
}

return _cachedGetValues;
}
}

private class HostingRequestFailed
Expand All @@ -207,20 +227,30 @@ public HostingRequestFailed(HttpContext httpContext, TimeSpan elapsed)
_elapsed = elapsed;
}

public override string ToString() => _cachedToString ?? Interlocked.CompareExchange(
ref _cachedToString,
$"Request finished in {_elapsed.TotalMilliseconds}ms 500",
null);
public override string ToString()
{
if (_cachedToString == null)
{
_cachedToString = $"Request finished in {_elapsed.TotalMilliseconds}ms 500";
}

public IEnumerable<KeyValuePair<string, object>> GetValues() => _cachedGetValues ?? Interlocked.CompareExchange(
ref _cachedGetValues,
new[]
return _cachedToString;
}

public IEnumerable<KeyValuePair<string, object>> GetValues()
{
if (_cachedGetValues == null)
{
new KeyValuePair<string, object>("ElapsedMilliseconds", _elapsed.TotalMilliseconds),
new KeyValuePair<string, object>("StatusCode", 500),
new KeyValuePair<string, object>("ContentType", null),
},
null);
_cachedGetValues = new[]
{
new KeyValuePair<string, object>("ElapsedMilliseconds", _elapsed.TotalMilliseconds),
new KeyValuePair<string, object>("StatusCode", 500),
new KeyValuePair<string, object>("ContentType", null),
};
}

return _cachedGetValues;
}
}
}
}
Expand Down

0 comments on commit 28e064f

Please sign in to comment.