“ILogger cannot be injected” 可以翻译为 “ILogger 无法被注入”。

huangapple go评论49阅读模式
英文:

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&lt;AccountController&gt;? _logger;

    public AccountController(IConfiguration? configuration, ILogger? logger)
    {
        _configuration = configuration;
        _logger = (ILogger&lt;AccountController&gt;?)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&lt;&gt;(即 ILogger&lt;AccountController&gt;),注入 ILogger 不受框架提供的库默认支持:

英文:

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

public class AccountController : ControllerBase
{
    private readonly IConfiguration? _configuration;
    private readonly ILogger&lt;AccountController&gt; _logger;

    public AccountController(IConfiguration? configuration, ILogger&lt;AccountController&gt; logger)
    {
        _configuration = configuration;
        _logger = logger;
    }
}

huangapple
  • 本文由 发表于 2023年5月25日 04:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定