I've configured my console application's Main
like so
var services = new ServiceCollection()
.AddLogging(logging => logging.AddConsole())
.BuildServiceProvider();
And then I try to use it in another class like so
private readonly ILogger _logger;
public MyClass(ILogger logger)
{
_logger = logger;
}
public void MyFunc()
{
_logger.Log(LogLevel.Error, "My Message");
}
System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger'
I've tried the solutions here but it didn't work for me.
Edit Based on Yaakov's comment below and this Github comment I'm able to resolve it correctly by doing this
public MyClass(ILogger<MyClass> logger)
{
_logger = logger;
}
I would have preferred to have this in the initial BuildServiceProvider
but looks like I'm gonna have to repeat this every time I want to use the logger (or create my own ILogger).
..AddLogging(logging => logging.AddConsole())
like.AddScoped<IMyInterface, MyMethod>()
which resolves correctly.ILogger<MyClass>
?public MyClass(ILogger<MyClass> logger)
every time?