Skip to content

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

Merged
merged 7 commits into from
Jun 6, 2019

Conversation

Kahbazi
Copy link
Member

@Kahbazi Kahbazi commented Jun 6, 2019

/// <see cref="RequestLocalizationMiddleware"/>.</param>
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options, ILoggerFactory loggerFactory)
Copy link
Member

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>.

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member Author

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!!?

Copy link
Member

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 😢

Copy link
Contributor

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

Copy link
Contributor

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:

  1. 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.
  2. Add [Obsolete] attribute to the current attribute. Add ActivatorUtilitiesConstructorAttribute to the new ctor
  3. Change the current constructor to call the other ctor. Have it pass a NullLogger instance. This should avoid the need to have EnsureLogger

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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?

Copy link
Member

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.

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jun 6, 2019
/// <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")]
Copy link
Contributor

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")]

@Tratcher
Copy link
Member

Tratcher commented Jun 6, 2019

So far so good. @pranavkm can you track merging this?

@pranavkm
Copy link
Contributor

pranavkm commented Jun 6, 2019

@Kahbazi 👍

src\Middleware\Localization\ref\Microsoft.AspNetCore.Localization.netcoreapp3.0.cs(,): error : Generated code is not up to date in src/Middleware/Localization/ref/Microsoft.AspNetCore.Localization.netcoreapp3.0.cs. You might need to regenerate the reference assemblies or project list (see docs/ReferenceAssemblies.md and docs/ReferenceResolution.md)
PowerShell exited with code '1'.

@Kahbazi
Copy link
Member Author

Kahbazi commented Jun 6, 2019

@pranavkm Done!

@pranavkm pranavkm merged commit ba8c6cc into dotnet:master Jun 6, 2019
@pranavkm
Copy link
Contributor

pranavkm commented Jun 6, 2019

Thanks for the PR!

@pranavkm pranavkm added this to the 3.0.0-preview7 milestone Jun 6, 2019
@Kahbazi Kahbazi deleted the RemoveEnsureLogger branch June 6, 2019 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants