WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
15 stars 3 forks source link

Analyzer to warn against mismatching ILogger type arguments #213

Open yaakov-h opened 1 year ago

yaakov-h commented 1 year ago

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 commented 1 year ago

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

yaakov-h commented 1 year ago

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