简体   繁体   English

带有Asp.Net Core 2.1的Serilog不能从appsettings.json写入

[英]Serilog with Asp.Net Core 2.1 not writing from appsettings.json

I am trying to configure Serilog to read my settings from the appsettings.json file in my ASP.NET Core 2 WebApi. 我正在尝试将Serilog配置为从ASP.NET Core 2 WebApi中的appsettings.json文件读取我的设置。

I have the following in the root of the appsetting.json file; 我在appsetting.json文件的根目录中有以下内容;

"Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Async", "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel":  "Information",
    "WriteTo": [
      {
        "Name": "Async",
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": { "pathFormat": "Logs\\Serilog\\log.txt" },
              "RollingInterval": "Day"
            },
            {
              "Name": "MSSqlServer",
              "Args": {
                "connectionString": "myconnectionstring",
                "schemaName": "Schema",
                "tableName": "Logs",
                "batchPostingLimit": 1000,
                "period": 30
              }
            }
          ]
        }
      }
    ]
  }

In my Startup.cs I have the following; 在我的Startup.cs我有以下内容;

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var log = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();

        loggerFactory.AddSerilog(log);

If I run the following then the output perfectly so I know its down to the move to appsettings; 如果我运行以下命令,那么输出将是完美的,因此我知道将其转移到appsettings的过程。

var log = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .WriteTo.MSSqlServer("connectionString", "Logs", schemaName:"Schema")
            .WriteTo.File("Logs\\Serilog\\log.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();

And then in my Program.cs I have; 然后在我的Program.cs中;

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(config =>
                config.AddJsonFile(
                    x =>
                    {
                        x.Path = "appsettings.json";
                        x.Optional = true;
                        x.ReloadOnChange = true;
                    })
                .AddJsonFile(
                    x =>
                    {
                        x.Path = "appsettings.{currentEnv}.json";
                        x.Optional = true;
                        x.ReloadOnChange = true;
                    })
            )
            .UseStartup<Startup>()
            .Build();

To note, both appsettings.json and the environment specific currently contain the same data. 注意,appsettings.json和特定于环境的当前都包含相同的数据。

I have the following versions of Serilog installed; 我安装了以下版本的Serilog;

Serilog 2.7.1 Serilog.spNetCore 2.1.1 Serilog.Enrichers.Environment 2.1.2 Serilog.Settings.Configuration 3.0.1 Serilog.Sinks.Async 1.3.0 Serilog.Sinks.File 4.0.0 Serilog.Sinks.MSSqlServer 5.1.2 Serilog 2.7.1 Serilog.spNetCore 2.1.1 Serilog.Enrichers.Environment 2.1.2 Serilog.Settings.Configuration 3.0.1 Serilog.Sinks.Async 1.3.0 Serilog.Sinks.File 4.0.0 Serilog.Sinks.MSSqlServer 5.1.2

Try this in your Program.cs file: 在Program.cs文件中尝试以下操作:

UseStartup<Startup>() .UseSerilog() .Build();

That will tell the HostBuilder to use the Serilog package. 这将告诉HostBuilder使用Serilog软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Serilog:RollingFile 在带有“appsettings.json”的 asp.net 核心中不起作用 - Serilog : RollingFile is not working in asp.net core with 'appsettings.json' asp.net core - 从 appsettings.json 设置 Serilog.Exceptions - asp.net core - Set Serilog.Exceptions from appsettings.json 将特定命名空间添加到 appsettings.json - Serilog ASP.NET Core 3.1 - Add Specfic Namespace to appsettings.json - Serilog ASP.NET Core 3.1 ASP.Net Core 2.1 应用程序在作为 Windows 服务运行时找不到 appsettings.json - ASP.Net Core 2.1 application cannot find appsettings.json when ran as a Windows service ASP.NET Core 2.1应用程序未获取正确的appsettings.json - ASP.NET Core 2.1 application not getting correct appsettings.json 使用ASP.NET Core反序列化appsettings.json - Deserialize appsettings.json with ASP.NET Core ASP.NET Core appsettings.json 更新代码 - ASP.NET Core appsettings.json update in code AppSettings.json用于ASP.NET核心中的集成测试 - AppSettings.json for Integration Test in ASP.NET Core ASP.NET Core appsettings.json需要在磁盘上更新 - ASP.NET Core appsettings.json need to update on disk ASP.NET核心EF代码首先appsettings.json - ASP.NET Core EF Code First appsettings.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM