-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Inject ILogger to RequestLocalization Middleware #10946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
/// <see cref="RequestLocalizationMiddleware"/>.</param> | ||
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options) | ||
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you take the change in 16739e0?
So to not inject the ILogger<RequestLocalizationMiddleware>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that other middlewares inject ILoggerFactory
so I wanted to keep the same pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The breaking change guidelines are to add a new constructor and obsolete the old one. For weird DI reasons the new constructor should be placed above the old one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So how should we create logger object for the old constructor. Maybe I should put back the EnsureLogger!!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For weird DI reasons the new constructor should be placed above the old one.
Does this actually matter to DI? the order used by reflection is not a stable guarantee 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an attribute that you can use to specify the constructor to be used - ActivatorUtilitiesConstructorAttribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kahbazi here's what I'd do:
- Add a new ctor which accepts a
ILoggerFactory
\ILogger<T>
instance. I personally prefer the latter, but there isn't a clear guidance for one over the other. - Add
[Obsolete]
attribute to the current attribute. AddActivatorUtilitiesConstructorAttribute
to the new ctor - Change the current constructor to call the other ctor. Have it pass a
NullLogger
instance. This should avoid the need to haveEnsureLogger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kahbazi here's an example, we use a NullLogger for the other constructor:
https://github.com/aspnet/AspNetCore/blob/62351067ff4c1401556725b401478e648b66acdc/src/Security/CookiePolicy/src/CookiePolicyMiddleware.cs#L20-L32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the log will be disabled if someone directly uses the old constructor, is that ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's quite unlikely anyone will use the old one, but we leave it for binary compat.
src/Middleware/Localization/src/RequestLocalizationMiddleware.cs
Outdated
Show resolved
Hide resolved
/// <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 is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Obsolete("This constructor is obsolete and will be removed in a future version. Use RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory) instead")]
So far so good. @pranavkm can you track merging this? |
@Kahbazi 👍
|
@pranavkm Done! |
Thanks for the PR! |
#6532