【问题标题】:Unable to resolve service for type 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware"无法解析“Serilog.ILogger”类型的服务:“AspNetCore.Serilog.RequestLoggingMiddleware”
【发布时间】:2019-08-13 04:04:02
【问题描述】:

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

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

APPSETTING.JSON

      {
          "Serilog": {
              "MinimumLevel": {
                  "Default": "Information",
                  "System": "Warning",
                  "Microsoft": "Information"
              },
              "File": {
                  "location": "logs/timeapi.log"
              }
          },
          "EnableSwagger": true
      }

程序.CS

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

STARTUP.CS

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

我得到的错误是:

Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AspNetCore.Serilog.RequestLoggingMiddleware.SerilogRequestMiddleware'.

【问题讨论】:

  • 你错过了.UseSerilog 吗?
  • @RubenBartelink 谢谢,添加后没有修复:(
  • 抱歉,没注意 - 一般情况下,UseSerilog 会注册一个 Microsoft.Extensions.Logging.ILogger&lt;T&gt; - 很可能在不添加 UseSerilog 的情况下也能正常工作。不幸的是,我没有玩太多,所以无法在 aspnetcore 文章中提供 Serilog 入门的链接:(
  • @RubenBartelink 这里的转折是它不是 Serilog.AspNetCore UseSerilogRequestLogging() 而是另一个我以前没见过的 impl

标签: c# asp.net-mvc .net-core serilog


【解决方案1】:

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

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ILogger>(Log.Logger);
    }

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

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

【讨论】:

    【解决方案2】:

    如果您使用的是 .net core 3.1 +,请仅在 program.cs 中使用它,并从 startup.cs 中删除有关 Serilog 的所有内容

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
        .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
        .WriteTo.Console())
        ......;
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        • 2022-01-10
        • 2020-03-13
        • 2019-02-10
        • 2019-04-27
        • 1970-01-01
        相关资源
        最近更新 更多