-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Missing argument validation on 'LoggerFactory.AddProvider' #45599
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsDescription
Some operations inside the class will unnecessarily throw One would expect that ConfigurationNet 5.0 Other informationHere is where it loads up the runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs Lines 148 to 157 in fae3594
runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs Lines 174 to 180 in fae3594
And here an example where exceptions will be generated and caught: runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs Lines 254 to 264 in fae3594
I'm not sure if this could cause further problems downstream when using the loggers from the factory.
|
As part of the effort to enable nullable annotations for extensions libraries (tracked here), we just recently annotated Logging.Abstractions, and that means ILoggerFactory implemented under Logging.Abstractions would actually expect to take a non-nullable ILoggerProvider for When we enable nullable annotations for Microsoft.Extensions.Logging, it should take this into account for the code snippets you share above as well. So I think, throwing ArgumentNullException would be mainly beneficial for when nullable annotations are not enabled. |
I think this is ill-advised @maryamariyan : This is because as of right now, there is no runtime validation leveraging nullable annotations, so if consumers pass a I do hope to see C# and the runtime move into full-on runtime validation of these nullable constraints at some point, and then we could remove the manual argument validation from everywhere. |
I am looking for a good first issue to pick up, does anyone have an issue if I do that ? |
Seems like there is a PR for this now. @Michael3161010 you could perhaps pick another one from here and comment on the one you picked stating you picked it and would be creating a PR for it. |
Thanks for the pointer @julealgon. We should throw even if nullable annotations are enabled, since the consuming library may not have nullable enabled. Also, e.g. cc: @safern |
Description
Microsoft.Extensions.Logging.LoggerFactory
implementsILoggerFactory
with aAddProvider(ILoggerProvider)
method. However, it does not perform any argument validation onprovider
, meaning it will considernull
as a "valid" provider and add it to the collection of providers internally.Some operations inside the class will unnecessarily throw
NullReferenceException
s due to this behavior, like theDispose
method, which will capture these exceptions and do nothing.One would expect that
null
providers are not accepted and the method should throw aArgumentNullException
.Configuration
Net 5.0
Microsoft.Extensions.Logging latest (v5.0.0)
Other information
Here is where it loads up the
provider
:runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs
Lines 148 to 157 in fae3594
runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs
Lines 174 to 180 in fae3594
And here an example where exceptions will be generated and caught:
runtime/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs
Lines 254 to 264 in fae3594
I'm not sure if this could cause further problems downstream when using the loggers from the factory.
The text was updated successfully, but these errors were encountered: