簡體   English   中英

使用 appsettings.json 配置 Serilog 以將某些日志過濾到不同的文件

[英]Configure Serilog to filter certain logs to a different file using appsettings.json

我有一個使用 Serilog 將日志數據寫入文件的應用程序。 我現在正在嘗試使用appsettings.json中的子記錄器配置我的記錄器,以便我可以將某些日志過濾到不同的文件中。 我知道我可以在代碼中配置記錄器,但我想通過appsettings.json來完成。

基本上,我希望所有日志到 go 到同一個文件,除了某種類型的日志。 我使用了Serilog wiki和一些博客文章和 stackoverflow 條目,主要是這個,來實現我的目標。從我讀過的內容來看,使用以下內容應該允許我過濾這種類型的日志條目:

using (LogContext.PushProperty("SpecialLogType", true)) {
    _logger.LogInformation("MyLogEntry {MyParam}", myParam);
}

我配置了兩個接收器,一個用於普通日志,一個用於這種特殊類型的日志。 使用過濾器,我現在應該能夠使用此屬性過濾日志。 但我無法弄清楚我需要如何在 appsettings.json 中配置子記錄器。 現在,我的appsettings.json看起來像這樣:

"Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Information",
    },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "@p['SpecialLogType'] = 'true'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/NormalTypeLog_.txt",
                  // other configuration
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "@p['SpecialLogType'] = 'true'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/SpecialTypeLog.json",
                  // other configuration
                }
              }
            ]
          }
        }
      }
    ]
  }

我已經嘗試了幾種不同的方法並取得了一些結果,但我無法讓它正常工作並且會提出一些建議。 有沒有人有任何提示?

所以幾天后我設法弄清楚了這一點,並想如果有類似問題的人偶然發現它,我會在這里發布我的完整解決方案。

我認為我走在正確的道路上,但我的主要問題是我錯誤地使用了Serilog 的表達式package。 我將在多個示例中找到的東西混合在一起,但最終沒有奏效。 更多閱讀 wiki 和nblumhardt 的博客文章引導我找到最終的解決方案。

通過 Serilog 的 appsettings.json 配置過濾日志事件到不同的文件

可以使用屬性豐富日志。 這些屬性可以稍后在管道中用於將日志過濾到不同的記錄器。 可以通過LogContext class 將屬性添加到日志中。

using (LogContext.PushProperty("SpecialLogType", true)) {
    _logger.LogInformation("This is a log entry that will be filtered to a different file");
}

要實際配置記錄器,您需要創建兩個子記錄器。 過濾必須發生在子記錄器級別 每個子記錄器都必須過濾它自己的事件。 在頂級記錄器進行過濾將過濾掉所有子記錄器的日志事件。 過濾是通過Serilog 的表達式package 完成的。

要僅包含附加屬性的日志事件,我執行了以下操作

"Name": "ByIncludingOnly",
"Args": {
  "expression": "SpecialLogTypeis not null"
}

由於我希望將 go 的所有其他事件添加到“標准”文件中,因此對於我的其他子記錄器,我只是排除了所有帶有附加屬性的日志事件

"Name": "ByExcluding",
"Args": {
  "expression": "SpecialLogTypeis not null"
}

完整的相關 appsettings.json 部分

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions" ],
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "Debug" },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByExcluding",
                "Args": {
                  "expression": "SpecialLogType is not null"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/NormalLogEvents_.txt",
                  // other irrelevant file configuration
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "SpecialLogType is not null"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/SpecialLoggingType_.log",
                  // other irrelevant file configuration
                }
              }
            ]
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  }

這對我有用,我希望它可以幫助任何有同樣問題的人。

也很有用: Serilog 設置配置自述文件

暫無
暫無

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

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