diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs index 50d5b010..b18f50c7 100644 --- a/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs +++ b/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs @@ -2,8 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Diagnostics; +using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; @@ -52,7 +53,7 @@ public static void ApplicationError(this ILogger logger, Exception exception) logger.LogError( eventId: LoggerEventIds.ApplicationStartupException, message: "Application startup exception", - error: exception); + exception: exception); } public static void Starting(this ILogger logger) @@ -61,7 +62,7 @@ public static void Starting(this ILogger logger) { logger.LogDebug( eventId: LoggerEventIds.Starting, - data: "Hosting starting"); + message: "Hosting starting"); } } @@ -71,7 +72,7 @@ public static void Started(this ILogger logger) { logger.LogDebug( eventId: LoggerEventIds.Started, - data: "Hosting started"); + message: "Hosting started"); } } @@ -81,17 +82,40 @@ public static void Shutdown(this ILogger logger) { logger.LogDebug( eventId: LoggerEventIds.Shutdown, - data: "Hosting shutdown"); + message: "Hosting shutdown"); } } - private class HostingLogScope : ILogValues + private class HostingLogScope : IReadOnlyList> { private readonly HttpContext _httpContext; private string _cachedToString; - private IEnumerable> _cachedGetValues; + + public int Count + { + get + { + return 2; + } + } + + public KeyValuePair this[int index] + { + get + { + if (index == 0) + { + return new KeyValuePair("RequestId", _httpContext.TraceIdentifier); + } + else if (index == 1) + { + return new KeyValuePair("RequestPath", _httpContext.Request.Path.ToString()); + } + throw new IndexOutOfRangeException(nameof(index)); + } + } public HostingLogScope(HttpContext httpContext) { @@ -108,29 +132,65 @@ public override string ToString() return _cachedToString; } - public IEnumerable> GetValues() + public IEnumerator> GetEnumerator() { - if (_cachedGetValues == null) + for (int i = 0; i < Count; ++i) { - _cachedGetValues = new[] - { - new KeyValuePair("RequestId", _httpContext.TraceIdentifier), - new KeyValuePair("RequestPath", _httpContext.Request.Path.ToString()), - }; + yield return this[i]; } + } - return _cachedGetValues; + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } - private class HostingRequestStarting : ILogValues + private class HostingRequestStarting : IReadOnlyList> { internal static readonly Func Callback = (state, exception) => ((HostingRequestStarting)state).ToString(); private readonly HttpRequest _request; private string _cachedToString; - private IEnumerable> _cachedGetValues; + + public int Count + { + get + { + return 9; + } + } + + public KeyValuePair this[int index] + { + get + { + switch (index) + { + case 0: + return new KeyValuePair("Protocol", _request.Protocol); + case 1: + return new KeyValuePair("Method", _request.Method); + case 2: + return new KeyValuePair("ContentType", _request.ContentType); + case 3: + return new KeyValuePair("ContentLength", _request.ContentLength); + case 4: + return new KeyValuePair("Scheme", _request.Scheme.ToString()); + case 5: + return new KeyValuePair("Host", _request.Host.ToString()); + case 6: + return new KeyValuePair("PathBase", _request.PathBase.ToString()); + case 7: + return new KeyValuePair("Path", _request.Path.ToString()); + case 8: + return new KeyValuePair("QueryString", _request.QueryString.ToString()); + default: + throw new IndexOutOfRangeException(nameof(index)); + } + } + } public HostingRequestStarting(HttpContext httpContext) { @@ -147,38 +207,55 @@ public override string ToString() return _cachedToString; } - public IEnumerable> GetValues() + public IEnumerator> GetEnumerator() { - if (_cachedGetValues == null) + for (int i = 0; i < Count; ++i) { - _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()), - }; + yield return this[i]; } + } - return _cachedGetValues; + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } - private class HostingRequestFinished + private class HostingRequestFinished : IReadOnlyList> { internal static readonly Func Callback = (state, exception) => ((HostingRequestFinished)state).ToString(); private readonly HttpContext _httpContext; private readonly TimeSpan _elapsed; - - private IEnumerable> _cachedGetValues; + private string _cachedToString; + public int Count + { + get + { + return 3; + } + } + + public KeyValuePair this[int index] + { + get + { + switch (index) + { + case 0: + return new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds); + case 1: + return new KeyValuePair("StatusCode", _httpContext.Response.StatusCode); + case 2: + return new KeyValuePair("ContentType", _httpContext.Response.ContentType); + default: + throw new IndexOutOfRangeException(nameof(index)); + } + } + } + public HostingRequestFinished(HttpContext httpContext, TimeSpan elapsed) { _httpContext = httpContext; @@ -195,19 +272,17 @@ public override string ToString() return _cachedToString; } - public IEnumerable> GetValues() + public IEnumerator> GetEnumerator() { - if (_cachedGetValues == null) + for (int i = 0; i < Count; ++i) { - _cachedGetValues = new[] - { - new KeyValuePair("ElapsedMilliseconds", _elapsed.TotalMilliseconds), - new KeyValuePair("StatusCode", _httpContext.Response.StatusCode), - new KeyValuePair("ContentType", _httpContext.Response.ContentType), - }; + yield return this[i]; } + } - return _cachedGetValues; + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } } diff --git a/src/Microsoft.AspNetCore.Server.Testing/Common/RetryHelper.cs b/src/Microsoft.AspNetCore.Server.Testing/Common/RetryHelper.cs index 1ab7075a..947a0b5d 100644 --- a/src/Microsoft.AspNetCore.Server.Testing/Common/RetryHelper.cs +++ b/src/Microsoft.AspNetCore.Server.Testing/Common/RetryHelper.cs @@ -51,7 +51,7 @@ public static async Task RetryRequest( { if (retry == retryCount - 1) { - logger.LogError("Failed to connect, retry limit exceeded.", exception); + logger.LogError(0, exception, "Failed to connect, retry limit exceeded."); throw; } else diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs index d34952b8..2415591e 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs @@ -611,7 +611,7 @@ public IDisposable BeginScopeImpl(object state) var stringified = state.ToString(); return this; } - public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { var stringified = formatter(state, exception); } diff --git a/test/Microsoft.AspNetCore.TestHost.Tests/ClientHandlerTests.cs b/test/Microsoft.AspNetCore.TestHost.Tests/ClientHandlerTests.cs index 8e71b284..2d36ee69 100644 --- a/test/Microsoft.AspNetCore.TestHost.Tests/ClientHandlerTests.cs +++ b/test/Microsoft.AspNetCore.TestHost.Tests/ClientHandlerTests.cs @@ -308,7 +308,7 @@ private class VerifierLogger : ILogger public bool IsEnabled(LogLevel logLevel) => true; // This call verifies that fields of HttpRequest are accessed and valid - public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) => formatter(state, exception); + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) => formatter(state, exception); class NoopDispoasble : IDisposable { diff --git a/test/Microsoft.AspNetCore.TestHost.Tests/TestClientTests.cs b/test/Microsoft.AspNetCore.TestHost.Tests/TestClientTests.cs index 4007f9c0..106a77f5 100644 --- a/test/Microsoft.AspNetCore.TestHost.Tests/TestClientTests.cs +++ b/test/Microsoft.AspNetCore.TestHost.Tests/TestClientTests.cs @@ -204,7 +204,7 @@ private class VerifierLogger : ILogger public bool IsEnabled(LogLevel logLevel) => true; // This call verifies that fields of HttpRequest are accessed and valid - public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) => formatter(state, exception); + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) => formatter(state, exception); class NoopDispoasble : IDisposable {