序列号自定义输出模板



我正在寻找用于记录的自定义输出模板格式

示例输出模板:"{\"时间\":\"+\",\"严重性\":"{Level:u}\","machine\":"{MachineName}\",\"x-Correlation-ID":\"{CorrelationID}\

它总是期望第一个字段值为"+"值,如果该字段不存在,则意味着它不会替换下一个属性值({Level:u}(。

对于上述模板输出:{"time":"+ ","severity":"INFORMATION","machine":"xxxxxx", "x-Correlation-ID":"e5b9c851-de56-42d9-b414-9d7108d2ebcf"}

如果第一个字段值不是"+"值,则输出如下:{"时间":"测试","严重性":"{Level:u}","机器":"xxxxxx","x-Correlation-ID":"f6133a7e-ea4f-4bde-8200-798d5346d3ce"}

用于记录WriteTo.Async的RollingFileAlternate接收器(w=>w.RollingFileAlternate(logFilePath.ToString((,outputTemplate:logOutputTemplate,fileSizeLimitBytes:rollingFilesSize,retainedFileCountLimit:null(

如何在不影响其他输出模板属性的情况下删除第一个属性。

为了创建所需的相应输出,您可能需要实现一个实现所需逻辑的自定义ITextFormatter

您可以看到默认的代码(CompactJsonFormatter.csRenderedCompactJsonFormatter.cs(是如何实现的,并根据需要调整代码

public class YourCustomJsonFormatter : ITextFormatter
{
public void Format(LogEvent logEvent, TextWriter output)
{
// ...
}
}

Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new YourCustomJsonFormatter())
.CreateLogger();

最新更新