c# - 无法解析类型 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware" 的服务

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

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

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

应用设置.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();

启动.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'.

最佳答案

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

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

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

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

关于c# - 无法解析类型 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware" 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470972/

相关文章:

asp.net-mvc - 如何在 ASP.NET MVC 的所有操作中安装操作过滤器?

c# - MVC 3 模型验证问题——疏忽或设计使然

html - 绕过父级 : setting div width to 100% of window width 的最大宽度

c# - 如何使用 ASP.NET Core Razor 页面预选下拉列表中的项目

asp.net-core - “DevExpress.Blazor.DxDataGridColumn”没有与名称 'DisplayFormatString' 匹配的属性

c# - .NET4 中未观察到的任务异常

c# - 使用单个 LINQ 查询使用 Entity Framework 删除多条记录

C# 更改 FlowLayoutPanel 中控件的位置

javascript - 两个具有相同名称的 Javascript 函数总是调用相同的参数化函数

Azure Function 给出错误 : System. 此平台不支持绘图