[英]Serilog custom outputTemplate
我正在尋找用於記錄的自定義 output 模板格式
示例 output 模板:"{\"time\":\"+ \",\"severity\":\"{Level:u}\",\"machine\":\"{MachineName}\", \" x-Correlation-ID\":\"{CorrelationID}\"}"
它總是期望第一個字段值為“+”值,如果該字段不存在意味着它不會替換下一個屬性值({Level:u})。
對於上述模板 output: {"time":"+ ","severity":"INFORMATION","machine":"xxxxxx", "x-Correlation-ID":"e5b9c851-de56-42d9-b414-9d7108d2ebcf"}
如果第一個字段值不是“+”值,則 output 如下:{“time”:“test”,“severity”:“{Level:u}”,“machine”:“xxxxxx”,“x-Correlation-ID ":"f6133a7e-ea4f-4bde-8200-798d5346d3ce"}
RollingFileAlternate sink 用於記錄 WriteTo.Async(w => w.RollingFileAlternate(logFilePath.ToString(), outputTemplate: logOutputTemplate, fileSizeLimitBytes: rollingFilesSize, reservedFileCountLimit: null))
如何在不影響其他 output 模板屬性的情況下刪除第一個屬性。
您可能必須實現一個自定義ITextFormatter
來實現您需要的邏輯,以便創建您想要的相應 output。
您可以查看默認的( CompactJsonFormatter.cs
和RenderedCompactJsonFormatter.cs
)是如何實現的,並根據您的需要調整代碼。
public class YourCustomJsonFormatter : ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
// ...
}
}
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new YourCustomJsonFormatter())
.CreateLogger();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.