-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Labels
Description
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:
- https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#create-logs
- https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#log-category
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.