Как получить путь к журналу serilog из файла конфигурации приложения? - PullRequest
0 голосов
/ 10 апреля 2020

У меня есть следующая конфигурация в моем appsettings.json

"ApplicationLog": {
"Serilog": {
  "MinimumLevel": "Information",
  "WriteTo": [
    {
      "Name": "File",
      "Args": {
        "path": "%PROGRAMDATA%\\Logs\\AppLog-.txt",
        "fileSizeLimitBytes": 10485760,
        "retainedFileCountLimit": 10,
        "rollOnFileSizeLimit": true,
        "rollingInterval": "Day",
        "outputTemplate": "== {Timestamp:yyyy-MM-dd HH:mm:ss} [{Number}] [{LogType}] [{ErrorCode}] [{Level}] {Message}{NewLine}{Exception}"
      }
    }
  ]
 }
}

Я хотел бы получить Path из этих настроек. Например, путь

"% PROGRAMDATA% \ Logs \ AppLog-.txt",

Я пытаюсь использовать его, используя c# code

private string GetConfigurationPath()
{
    string pointer = "ApplicationLog:Serilog:WriteTo";
    string path = this._configuration.GetSection(pointer).Get<Dictionary<string, string>>().GetSection("Args:Path");
}

Но, невозможно получить его.

Также пробовал,

  string  a =  this._configuration.GetSection("ApplicationLog").GetSection("Serilog")["WriteTo"].ToString();

Выдает нулевое исключение!

В быстром просмотре я вижу как ниже

enter image description here

Ответы [ 2 ]

0 голосов
/ 12 апреля 2020

Я смог прочитать значение следующим образом

string path  = this.GetConfigurationNestedValue("ApplicationLog:Serilog:WriteTo:0:Args:path");


private string GetConfigurationNestedValue(string pointer)
{
    return this._configuration.GetSection(pointer).Value;
}
0 голосов
/ 10 апреля 2020

Я не знаю правильный синтаксис, но он должен выглядеть примерно так:

configuration
   .GetSection("ApplicationLog")
   .GetSection("Serilog")
   ["WriteTo"]
   .First()
   .GetSection("Args")
   ["path"]

Посмотрите на json! Это неправильно: Serilog:WriteTo", потому что WriteTo - это не раздел (или json объект), а массив!

...