Skip to content
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

Analyzer to warn against mismatching ILogger type arguments #213

Open
yaakov-h opened this issue Aug 3, 2023 · 2 comments
Open

Analyzer to warn against mismatching ILogger type arguments #213

yaakov-h opened this issue Aug 3, 2023 · 2 comments

Comments

@yaakov-h
Copy link
Member

yaakov-h commented Aug 3, 2023

When using Microsoft.Extensions.Logging and Dependency Injection, a type T should typically be injected with ILogger<T>.

For example:

class MyCoolClass
{
    public MyCoolClass(ILogger<MyCoolClass> logger) { }
}

This automatically configures the Category of the logs produced by this logger:

In an inheritance tree, I've seen a lot of people lazily satisfy the compiler and intellisense by passing in ILogger<base> instead, e.g.:

abstract class MyBaseClass
{
    public MyBaseClass(ILogger<MyBaseClass> logger) { }
}

class MyDerivedClass
{
    public MyDerivedClass(ILogger<MyBaseClass> logger) { } // This should be ILogger<MyDerivedClass>
}

We should detect this with a diagnostic and code fix.

@brian-reichle
Copy link
Contributor

Microsoft.Extensions.Logging feels like it might be a little too far removed from the "standard framework" for us to manage here.

@yaakov-h
Copy link
Member Author

yaakov-h commented Aug 3, 2023

It's part of the standard framework in ASP.NET Core applications, and part of dotnet/runtime, so it depends how you count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants