在docker yaml中覆盖serilog appsettings.json

时间:2018-10-03 07:26:55

标签: yaml serilog appsettings

让我们说我在YAML中有下一个Serilog配置:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.ApplicationInsights", 
        "Serilog.Sinks.Seq", "Serilog.Sinks.RollingFile", "Serilog.Sinks.Syslog" 
         ],
    "MinimumLevel": {
      "Default": "Warning",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Microsoft.AspNetCore.Authentication": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext} 
               {NewLine}Request ID: {RequestId}{NewLine}{Message:lj}{NewLine} 
                {Exception}"
        }
      }
      ,{
        "Name": "Syslog",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext} 
                  {NewLine}Request ID: {RequestId}{NewLine}{Message:lj}{NewLine} 
                  {Exception}",
          "server": "logsN.papertrailapp.com",
          "port": "XXXXX",
          "application": "My Application",
          "facility": "Local6"
        }
      }
      ,{
        "Name": "RollingFile",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext} 
                {NewLine}Request ID: {RequestId}{NewLine}{Message:lj} 
                {NewLine}{Exception}",
          "pathFormat": "Logs\\log-{Date}.txt"
        }
      }
      ,{
        "Name": "ApplicationInsights"
      }
      {
        "Name": "Seq",
        "Args": {
          "serverUrl": ""
        }
      }
    ],
    "Enrich": [ "FromLogContext" ],
    "Properties": {
      "Application": "My Application"
    }
  }

这是有效的配置,并且可以在控制台和docker中运行。
但我想有可能覆盖一些值(主要是目录(pathFormat)用于滚动文件和Serilog服务器)以及端口(用于yaml文件)。现在,我就像添加环境变量一样,并且在调用

之后
var loggerConfiguration = new LoggerConfiguration()        .ReadFrom.Configuration(Configuration);

我叫下一件事:

var appInsightsInstrumentationKey = Configuration.GetValue<string>("AppInsights_InstrumentationKey");
    if (!string.IsNullOrEmpty(appInsightsInstrumentationKey))
    {
        loggerConfiguration.WriteTo
          .ApplicationInsightsEvents(appInsightsInstrumentationKey);
    }

最后进入docker.yaml(简短版本,不包括所有值):

services:
    myapplication:
        environment:
            APPINSIGHTS_INSTRUMENTATIONKEY: "xxxxxx-xxxxxx-xxxxxx-xxxxxx"

我尝试使用下一个YAML配置访问Serilog设置:

services:
    myapplication:
        environment:
            Serilog:
                WriteTo:
                   -Name: "Syslog"
                   Args:
                      port:"xxxxx"

但是我得到一个错误:expected <block end> but found <block mapping start>(指向“ -Name:“ Syslog”“行)。

如何在YAML中正确映射数组。

0 个答案:

没有答案