簡體   English   中英

如何從應用程序配置文件中獲取 serilog 日志路徑?

[英]How to get the serilog log path from application configuration file?

我的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# 代碼

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();

它會引發 null 異常!

快速查看,我可以看到如下

在此處輸入圖像描述

我不知道正確的語法,但它應該是這樣的:

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

看看 json:這是錯誤的: Serilog:WriteTo"因為WriteTo不是一個部分(或 json 對象)它是一個數組!

我能夠讀取如下值

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


private string GetConfigurationNestedValue(string pointer)
{
    return this._configuration.GetSection(pointer).Value;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM