In a .Net 5 Web API, I'm trying to do logging in a scoped service.
I first tried to introduce the logger service via constructor dependency injection:
using Microsoft.Extensions.Logging; public class SmsService : ISmsService { private readonly ILogger _logger; public SmsService(ILogger logger) { _logger = logger; } } But when I run the API, I get this exception:
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: PropWorx.API.Interfaces.ISmsService Lifetime: Scoped ImplementationType: PropWorx.API.Services.SmsService': Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'PropWorx.API.Services.SmsService'.)'
I then removed the dependency injection, and tried creating a scope at the point of logging:
private readonly IServiceProvider _serviceProvider; using (var scope = _serviceProvider.CreateScope()) { using (var logger = scope.ServiceProvider.GetRequiredService<ILogger>()) { logger.LogError("some error message"); } } But I get this compile time error:
CS1674 'ILogger': type used in a using statement must be implicitly convertible to 'System.IDisposable'.
Any ideas?