Serilog ReadFrom配置

问题描述 投票:2回答:2

我试图使用Serilog.Settings.Configuration预发布NuGet包从appsettings.json加载我的Serilog配置。但是,当我使用Configuration.GetSection加载它时,该值继续返回null。

这是我在Program.cs文件中的内容:

return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(
    (hostingContext, loggingBuilder) =>
    {
        loggingBuilder.ClearProviders();
        loggingBuilder.AddSerilog(
            new LoggerConfiguration()
                .ReadFrom.ConfigurationSection(hostingContext.Configuration.GetSection("Serilog"))
                .CreateLogger()
        );
    }
);

这是我在appsettings.json文件中的内容:

{
"Serilog": {
    "Using": [ "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": {
        "Default": "Verbose",
        "Override": {
            "Microsoft": "Verbose",
            "System": "Verbose",
            "Microsoft.AspNetCore.Authentication": "Verbose"
        }
    },
    "WriteTo": [
        {
            "Name": "MSSqlServer",
            "Args": {
                "connectionString": "*my_connectionString*",
                "tableName": "ErrorLog"
            }
        }
    ]
}}

安装NuGet包:

  • Microsoft.AspNetCore.App v2.1.1
  • Serilog v2.7.1
  • Serilog.AspNetCore v2.1.1
  • Serilog.Extensions.Logging v2.0.2
  • Serilog.Settings.Configuration v3.0.0-dev-00119
  • Serilog.Sinks.MSSqlServer v5.1.2
logging asp.net-core .net-core serilog
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.