英文:
ILogger cannot be injected
问题
我有一个.Net 7核心项目,在记录方面运行良好。但是,我现在遇到了这个问题。
我有这个控制器:
public class AccountController : ControllerBase
{
private readonly IConfiguration? _configuration;
private readonly ILogger<AccountController>? _logger;
public AccountController(IConfiguration? configuration, ILogger<AccountController>? logger)
{
_configuration = configuration;
_logger = logger;
}
}
当我运行应用程序时,抛出以下异常:
System.InvalidOperationException: 尝试激活 'Modules.Authenticate.Core.Controllers.Api.AccountController' 时,无法解析类型 'Microsoft.Extensions.Logging.ILogger'。在此期间,发生以下异常:'Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)' 在 lambda_method9(Closure, IServiceProvider, Object[]) 在 Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.
g__CreateController|0(ControllerContext controllerContext) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- 前一位置的堆栈跟踪的结尾 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) 在 Microsoft.AspNetCore.Routing.EndpointMiddleware. g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
ILogger服务不是应该自动添加的吗?我该如何修复这个问题?
谢谢。
英文:
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.<CreateControllerFactory>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.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker
> invoker, Task lastTask, State next, Scope scope, Object state, Boolean
> isCompleted) at
> Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker
> invoker, Task task, IDisposable scope) at
> Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker
> invoker, Task task, IDisposable scope) at
> Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>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
答案1
得分: 1
只解决封闭的泛型 ILogger<>
(即 ILogger<AccountController>
),注入 ILogger
不受框架提供的库默认支持:
英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论