简体   繁体   English

使用 Net Core 2.2 覆盖 appsettings.production 中的 Serilog 设置

[英]Serilog override settings in appsettings.production with Net Core 2.2

I have an issue with serilog when i want to write to a database based on different environments.当我想根据不同的环境写入数据库时,我遇到了 serilog 的问题。 Serilog override my appsettings.production.json and always take the settings in appsettings.json instead even in a production mode ! Serilog 覆盖我的 appsettings.production.json 并始终采用 appsettings.json 中的设置,即使在生产模式下也是如此!

My code in my local appsettings.json我在本地appsettings.json中的代码

"Serilog": {
        "Using": [ "Serilog.Sinks.MSSqlServer" ],
        "MinimumLevel": {
            "Default": "Debug",
            "Override": {
                "Microsoft": "Warning",
                "System": "Warning"
            }
        },
        "WriteTo": {
          "Console" : {"Name": "Console"},
          "Sql": {
             "Name": "MSSqlServer",
             "Args": {
                "connectionString": "Server=MyServerInDev\\SQLSERVER;Database=MyDBDev;user id=ui;password=pw_;ConnectRetryCount=0;MultipleActiveResultSets=true",
                "tableName": "Log",
                "autoCreateSqlTable": true
            }
        }
    }, 

appsettings.production.json appsettings.production.json

"Serilog": {
        "Using": [ "Serilog.Sinks.MSSqlServer" ],
        "MinimumLevel": {
            "Default": "Warning",
            "Override": {
                "Microsoft": "Warning",
                "System": "Warning"
            }
        },
        "WriteTo": {
          "Console" : {"Name": "Console"},
          "Sql": {
             "Name": "MSSqlServer",
             "Args": {
                "connectionString": "Server=MyServerInProd\\SQLSERVER;Database=MyDBProdv;user id=ui;password=pw_;ConnectRetryCount=0;MultipleActiveResultSets=true",
                "tableName": "Log",
                "autoCreateSqlTable": true
            }
        }
    },

My program.cs我的程序.cs

var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

I am missing something?我错过了什么? and why serilog take the appsettings.json settings and not appsettings.production.json even in prod mode以及为什么 serilog 采用 appsettings.json 设置而不是 appsettings.production.json 即使在 prod 模式下

Note that the other settings in the appsettings.production.json work fine.请注意,appsettings.production.json 中的其他设置工作正常。 Only the settings in serilog is causing the problem只有 serilog 中的设置会导致问题

Found it.找到了。 The reason is that Serilog adds sink configs to array, and each of two files of appsettings just adds another sinks instead of replacing them.原因是 Serilog 将接收器配置添加到数组中,并且 appsettings 的两个文件中的每一个都只是添加了另一个接收器而不是替换它们。 Solution is explicitly indicate array indexes for each sink.解决方案是明确指出每个接收器的数组索引。 Credits for answer: nblumhardt .答案的学分: nblumhardt Tested, works.经测试,有效。

"WriteTo": {
  "0": {
    "Name": "File",
    "Args": {
      "path": "./logs/myapp-.txt",
      "rollingInterval": "Day"
    }
  }
},

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM