没有注册类型'Microsoft.Extensions.Logging.ILogger'的服务

blu*_*nha 3 asp.net-core

创建了一个空白的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");
            };
        });
Run Code Online (Sandbox Code Playgroud)

但是,当我构建项目时,它抱怨

 An error occurred while starting the application.
 InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.
Run Code Online (Sandbox Code Playgroud)

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

services.AddScope
Run Code Online (Sandbox Code Playgroud)

在ConfigureServices中

Joe*_*tte 6

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");
            };
        });
Run Code Online (Sandbox Code Playgroud)