From ba8c6ccf6fd3eeb7fc42a159d362b15eae4fb3a0 Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Fri, 7 Jun 2019 03:08:02 +0430 Subject: [PATCH] Inject ILogger to RequestLocalization Middleware (#10946) --- ...t.AspNetCore.Localization.netcoreapp3.0.cs | 3 ++ .../src/RequestLocalizationMiddleware.cs | 33 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs b/src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs index dd739afbe4..599db4aca6 100644 --- a/src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs +++ b/src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs @@ -99,7 +99,10 @@ namespace Microsoft.AspNetCore.Localization } public partial class RequestLocalizationMiddleware { + [System.ObsoleteAttribute("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions options, ILoggerFactory loggerFactory) instead")] public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options) { } + [Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute] + public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { } [System.Diagnostics.DebuggerStepThroughAttribute] public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } } diff --git a/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs b/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs index 7c2462a4b7..f91260f63d 100644 --- a/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs +++ b/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; @@ -26,15 +27,17 @@ namespace Microsoft.AspNetCore.Localization private readonly RequestDelegate _next; private readonly RequestLocalizationOptions _options; - private ILogger _logger; + private readonly ILogger _logger; /// /// Creates a new . /// /// The representing the next middleware in the pipeline. /// The representing the options for the + /// The used for logging. /// . - public RequestLocalizationMiddleware(RequestDelegate next, IOptions options) + [ActivatorUtilitiesConstructor] + public RequestLocalizationMiddleware(RequestDelegate next, IOptions options, ILoggerFactory loggerFactory) { if (options == null) { @@ -42,9 +45,22 @@ namespace Microsoft.AspNetCore.Localization } _next = next ?? throw new ArgumentNullException(nameof(next)); + _logger = loggerFactory?.CreateLogger() ?? throw new ArgumentNullException(nameof(loggerFactory)); _options = options.Value; } + /// + /// Creates a new . + /// + /// The representing the next middleware in the pipeline. + /// The representing the options for the + /// . + [Obsolete("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions options, ILoggerFactory loggerFactory) instead")] + public RequestLocalizationMiddleware(RequestDelegate next, IOptions options) + : this(next, options, NullLoggerFactory.Instance) + { + } + /// /// Invokes the logic of the middleware. /// @@ -84,8 +100,7 @@ namespace Microsoft.AspNetCore.Localization if (cultureInfo == null) { - EnsureLogger(context); - _logger?.UnsupportedCultures(provider.GetType().Name, cultures); + _logger.UnsupportedCultures(provider.GetType().Name, cultures); } } @@ -98,8 +113,7 @@ namespace Microsoft.AspNetCore.Localization if (uiCultureInfo == null) { - EnsureLogger(context); - _logger?.UnsupportedUICultures(provider.GetType().Name, uiCultures); + _logger.UnsupportedUICultures(provider.GetType().Name, uiCultures); } } @@ -136,11 +150,6 @@ namespace Microsoft.AspNetCore.Localization await _next(context); } - private void EnsureLogger(HttpContext context) - { - _logger = _logger ?? context.RequestServices.GetService>(); - } - private static void SetCurrentThreadCulture(RequestCulture requestCulture) { CultureInfo.CurrentCulture = requestCulture.Culture; @@ -212,4 +221,4 @@ namespace Microsoft.AspNetCore.Localization return culture; } } -} \ No newline at end of file +}