尚未注册“Microsoft.Extensions.Logging.ILogger”类型的服务

2023-12-23

创建了一个空白的 ASP.NET Core 2.0 应用程序。 在 Startup.cs 中,想要记录传入的请求。所以在配置方法中,我使用 Microsoft.Extensions.Logging.ILogger

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger)
  {    
      app.Use(next =>
        {
            return async context =>
            {
                logger.LogInformation("Incoming request");
                await next(context);
                logger.LogInformation("Outgoing response");
            };
        });

然而,当我构建项目时,它抱怨

 An error occurred while starting the application.
 InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.

为什么以及如何注册此服务?如果这是我的界面,那么这样做仍然有意义

services.AddScope

在配置服务中


ILogger 始终是一种类型,请尝试像这样更改它:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
  {    
      app.Use(next =>
        {
            return async context =>
            {
                logger.LogInformation("Incoming request");
                await next(context);
                logger.LogInformation("Outgoing response");
            };
        });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尚未注册“Microsoft.Extensions.Logging.ILogger”类型的服务 的相关文章

随机推荐