无法解析“Serilog.ILogger”类型的服务:“AspNetCore.Serilog.RequestLoggingMiddleware”

Utp*_*esh 7 c# asp.net-mvc serilog .net-core

我是编程新手,我正在尝试为 .net 核心项目实现日志记录解决方案。

我想收集默认和 Microsoft 类别日志。

APPSETTING.JSON

      {
          "Serilog": {
              "MinimumLevel": {
                  "Default": "Information",
                  "System": "Warning",
                  "Microsoft": "Information"
              },
              "File": {
                  "location": "logs/timeapi.log"
              }
          },
          "EnableSwagger": true
      }
Run Code Online (Sandbox Code Playgroud)

程序.CS

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
                    .UseSerilogRequestLogging();
Run Code Online (Sandbox Code Playgroud)

启动文件

app.UseSerilogRequestLogging(options =>
            options.RequestProjection =
            r => new { r.IsHttps, QueryString = r.QueryString.Value });
        app.UseMvc();
Run Code Online (Sandbox Code Playgroud)
https://github.com/mthamil/AspNetCore.Serilog.RequestLoggingMiddleware
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AspNetCore.Serilog.RequestLoggingMiddleware.SerilogRequestMiddleware'.
Run Code Online (Sandbox Code Playgroud)

Nic*_*rdt 10

您使用的中间件似乎需要ILogger在应用程序的Startup.ConfigureServices()方法中添加Serilog ;例如:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ILogger>(Log.Logger);
    }
Run Code Online (Sandbox Code Playgroud)

(此示例假设您正在配置 Serilog 的Log.Logger静态属性。)

Serilog.AspNetCore的自述文件中有一个更完整的使用 Serilog 设置请求日志记录的示例,尽管它的实现UseSerilogRequestLogging()完全不同。