0

I have a .Net 7 core project which worked well concerning logging. However, I am having this problem now.

I have this controller:

public class AccountController : ControllerBase
{
    private readonly IConfiguration? _configuration;
    private readonly ILogger<AccountController>? _logger;

    public AccountController(IConfiguration? configuration, ILogger? logger)
    {
        _configuration = configuration;
        _logger = (ILogger<AccountController>?)logger;
    }
}

When I run the application, this exception is thrown:

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'Modules.Authenticate.Core.Controllers.Api.AccountController'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method9(Closure, IServiceProvider, Object[]) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Isn't ILogger service supposed to be added automatically? How can I fix this?

Thanks

jstuardo
  • 3,901
  • 14
  • 61
  • 136

1 Answers1

1

Just resolve the closed generic ILogger<> (i.e. ILogger<AccountController>), injecting ILogger is not supported by framework provided libraries out of the box:

public class AccountController : ControllerBase
{
    private readonly IConfiguration? _configuration;
    private readonly ILogger<AccountController> _logger;

    public AccountController(IConfiguration? configuration, ILogger<AccountController> logger)
    {
        _configuration = configuration;
        _logger = logger;
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132