11
11
using Microsoft . AspNetCore . Localization . Internal ;
12
12
using Microsoft . Extensions . DependencyInjection ;
13
13
using Microsoft . Extensions . Logging ;
14
+ using Microsoft . Extensions . Logging . Abstractions ;
14
15
using Microsoft . Extensions . Options ;
15
16
using Microsoft . Extensions . Primitives ;
16
17
@@ -26,25 +27,40 @@ public class RequestLocalizationMiddleware
26
27
27
28
private readonly RequestDelegate _next ;
28
29
private readonly RequestLocalizationOptions _options ;
29
- private ILogger _logger ;
30
+ private readonly ILogger _logger ;
30
31
31
32
/// <summary>
32
33
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
33
34
/// </summary>
34
35
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
35
36
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
37
+ /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param>
36
38
/// <see cref="RequestLocalizationMiddleware"/>.</param>
37
- public RequestLocalizationMiddleware ( RequestDelegate next , IOptions < RequestLocalizationOptions > options )
39
+ [ ActivatorUtilitiesConstructor ]
40
+ public RequestLocalizationMiddleware ( RequestDelegate next , IOptions < RequestLocalizationOptions > options , ILoggerFactory loggerFactory )
38
41
{
39
42
if ( options == null )
40
43
{
41
44
throw new ArgumentNullException ( nameof ( options ) ) ;
42
45
}
43
46
44
47
_next = next ?? throw new ArgumentNullException ( nameof ( next ) ) ;
48
+ _logger = loggerFactory ? . CreateLogger < RequestLocalizationMiddleware > ( ) ?? throw new ArgumentNullException ( nameof ( loggerFactory ) ) ;
45
49
_options = options . Value ;
46
50
}
47
51
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
+
48
64
/// <summary>
49
65
/// Invokes the logic of the middleware.
50
66
/// </summary>
@@ -84,8 +100,7 @@ public async Task Invoke(HttpContext context)
84
100
85
101
if ( cultureInfo == null )
86
102
{
87
- EnsureLogger ( context ) ;
88
- _logger ? . UnsupportedCultures ( provider . GetType ( ) . Name , cultures ) ;
103
+ _logger . UnsupportedCultures ( provider . GetType ( ) . Name , cultures ) ;
89
104
}
90
105
}
91
106
@@ -98,8 +113,7 @@ public async Task Invoke(HttpContext context)
98
113
99
114
if ( uiCultureInfo == null )
100
115
{
101
- EnsureLogger ( context ) ;
102
- _logger ? . UnsupportedUICultures ( provider . GetType ( ) . Name , uiCultures ) ;
116
+ _logger . UnsupportedUICultures ( provider . GetType ( ) . Name , uiCultures ) ;
103
117
}
104
118
}
105
119
@@ -136,11 +150,6 @@ public async Task Invoke(HttpContext context)
136
150
await _next ( context ) ;
137
151
}
138
152
139
- private void EnsureLogger ( HttpContext context )
140
- {
141
- _logger = _logger ?? context . RequestServices . GetService < ILogger < RequestLocalizationMiddleware > > ( ) ;
142
- }
143
-
144
153
private static void SetCurrentThreadCulture ( RequestCulture requestCulture )
145
154
{
146
155
CultureInfo . CurrentCulture = requestCulture . Culture ;
@@ -212,4 +221,4 @@ private static CultureInfo GetCultureInfo(
212
221
return culture ;
213
222
}
214
223
}
215
- }
224
+ }
0 commit comments