Inject ILogger to RequestLocalization Middleware (#10946)

This commit is contained in:
Kahbazi 2019-06-07 03:08:02 +04:30 committed by Pranav K
parent 7e162096a9
commit ba8c6ccf6f
2 changed files with 24 additions and 12 deletions

View File

@ -99,7 +99,10 @@ namespace Microsoft.AspNetCore.Localization
} }
public partial class RequestLocalizationMiddleware public partial class RequestLocalizationMiddleware
{ {
[System.ObsoleteAttribute("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")]
public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> options) { } public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> options) { }
[Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute]
public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
[System.Diagnostics.DebuggerStepThroughAttribute] [System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; } public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
} }

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization.Internal; using Microsoft.AspNetCore.Localization.Internal;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@ -26,15 +27,17 @@ namespace Microsoft.AspNetCore.Localization
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly RequestLocalizationOptions _options; private readonly RequestLocalizationOptions _options;
private ILogger _logger; private readonly ILogger _logger;
/// <summary> /// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>. /// Creates a new <see cref="RequestLocalizationMiddleware"/>.
/// </summary> /// </summary>
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param> /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the /// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
/// <see cref="RequestLocalizationMiddleware"/>.</param> /// <see cref="RequestLocalizationMiddleware"/>.</param>
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options) [ActivatorUtilitiesConstructor]
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory)
{ {
if (options == null) if (options == null)
{ {
@ -42,9 +45,22 @@ namespace Microsoft.AspNetCore.Localization
} }
_next = next ?? throw new ArgumentNullException(nameof(next)); _next = next ?? throw new ArgumentNullException(nameof(next));
_logger = loggerFactory?.CreateLogger<RequestLocalizationMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
_options = options.Value; _options = options.Value;
} }
/// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
/// </summary>
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
/// <see cref="RequestLocalizationMiddleware"/>.</param>
[Obsolete("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")]
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
: this(next, options, NullLoggerFactory.Instance)
{
}
/// <summary> /// <summary>
/// Invokes the logic of the middleware. /// Invokes the logic of the middleware.
/// </summary> /// </summary>
@ -84,8 +100,7 @@ namespace Microsoft.AspNetCore.Localization
if (cultureInfo == null) 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) 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); await _next(context);
} }
private void EnsureLogger(HttpContext context)
{
_logger = _logger ?? context.RequestServices.GetService<ILogger<RequestLocalizationMiddleware>>();
}
private static void SetCurrentThreadCulture(RequestCulture requestCulture) private static void SetCurrentThreadCulture(RequestCulture requestCulture)
{ {
CultureInfo.CurrentCulture = requestCulture.Culture; CultureInfo.CurrentCulture = requestCulture.Culture;
@ -212,4 +221,4 @@ namespace Microsoft.AspNetCore.Localization
return culture; return culture;
} }
} }
} }