Skip to content

Analyzer to warn against mismatching ILogger type arguments #213

@yaakov-h

Description

@yaakov-h

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions