This is an example of one of my Web API controllers (for clarity, its methods are not shown):
public class TestController : ControllerBase
{
private readonly ILogger<TestController> _logger;
public TestController(ILogger<TestController> logger)
{
_logger = logger;
}
}
I would like to use a custom base controller, from which other controllers will derive. This controller would provide logging object for all other controllers. Here's my new code:
public class TestController : AppControllerBase<TestController>
{
public TestController()
{
}
}
public class AppControllerBase<T> : ControllerBase // compilation error here
{
protected readonly ILogger<T> _logger;
public AppControllerBase(ILogger<T> logger)
{
_logger = logger;
}
}
However this results in compilation error:
CS7036
There is no argument given that corresponds to the required formal parameter 'logger' of 'AppControllerBase.AppControllerBase(ILogger)
I would expect DI to provide ILogger<TestController>
in the base class constructor, but it seems not to be the case.
How could I resolve this?