Skip to content

Commit ba8c6cc

Browse files
Kahbazipranavkm
authored andcommitted
Inject ILogger to RequestLocalization Middleware (#10946)
1 parent 7e16209 commit ba8c6cc

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ protected RequestCultureProvider() { }
9999
}
100100
public partial class RequestLocalizationMiddleware
101101
{
102+
[System.ObsoleteAttribute("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")]
102103
public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> options) { }
104+
[Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute]
105+
public RequestLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Builder.RequestLocalizationOptions> options, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
103106
[System.Diagnostics.DebuggerStepThroughAttribute]
104107
public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
105108
}

src/Middleware/Localization/src/RequestLocalizationMiddleware.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Localization.Internal;
1212
using Microsoft.Extensions.DependencyInjection;
1313
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Logging.Abstractions;
1415
using Microsoft.Extensions.Options;
1516
using Microsoft.Extensions.Primitives;
1617

@@ -26,25 +27,40 @@ public class RequestLocalizationMiddleware
2627

2728
private readonly RequestDelegate _next;
2829
private readonly RequestLocalizationOptions _options;
29-
private ILogger _logger;
30+
private readonly ILogger _logger;
3031

3132
/// <summary>
3233
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
3334
/// </summary>
3435
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
3536
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
37+
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
3638
/// <see cref="RequestLocalizationMiddleware"/>.</param>
37-
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
39+
[ActivatorUtilitiesConstructor]
40+
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory)
3841
{
3942
if (options == null)
4043
{
4144
throw new ArgumentNullException(nameof(options));
4245
}
4346

4447
_next = next ?? throw new ArgumentNullException(nameof(next));
48+
_logger = loggerFactory?.CreateLogger<RequestLocalizationMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
4549
_options = options.Value;
4650
}
4751

52+
/// <summary>
53+
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
54+
/// </summary>
55+
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
56+
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
57+
/// <see cref="RequestLocalizationMiddleware"/>.</param>
58+
[Obsolete("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")]
59+
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
60+
: this(next, options, NullLoggerFactory.Instance)
61+
{
62+
}
63+
4864
/// <summary>
4965
/// Invokes the logic of the middleware.
5066
/// </summary>
@@ -84,8 +100,7 @@ public async Task Invoke(HttpContext context)
84100

85101
if (cultureInfo == null)
86102
{
87-
EnsureLogger(context);
88-
_logger?.UnsupportedCultures(provider.GetType().Name, cultures);
103+
_logger.UnsupportedCultures(provider.GetType().Name, cultures);
89104
}
90105
}
91106

@@ -98,8 +113,7 @@ public async Task Invoke(HttpContext context)
98113

99114
if (uiCultureInfo == null)
100115
{
101-
EnsureLogger(context);
102-
_logger?.UnsupportedUICultures(provider.GetType().Name, uiCultures);
116+
_logger.UnsupportedUICultures(provider.GetType().Name, uiCultures);
103117
}
104118
}
105119

@@ -136,11 +150,6 @@ public async Task Invoke(HttpContext context)
136150
await _next(context);
137151
}
138152

139-
private void EnsureLogger(HttpContext context)
140-
{
141-
_logger = _logger ?? context.RequestServices.GetService<ILogger<RequestLocalizationMiddleware>>();
142-
}
143-
144153
private static void SetCurrentThreadCulture(RequestCulture requestCulture)
145154
{
146155
CultureInfo.CurrentCulture = requestCulture.Culture;
@@ -212,4 +221,4 @@ private static CultureInfo GetCultureInfo(
212221
return culture;
213222
}
214223
}
215-
}
224+
}

0 commit comments

Comments
 (0)