简体   繁体   English

Serilog 自定义输出模板

[英]Serilog custom outputTemplate

I was looking for custom output template format for logging我正在寻找用于记录的自定义 output 模板格式

sample output template: "{\"time\":\"+ \",\"severity\":\"{Level:u}\",\"machine\":\"{MachineName}\", \"x-Correlation-ID\":\"{CorrelationID}\"}"示例 output 模板:"{\"time\":\"+ \",\"severity\":\"{Level:u}\",\"machine\":\"{MachineName}\", \" x-Correlation-ID\":\"{CorrelationID}\"}"

It is always expecting first filed value as "+" value, if that filed not exists means it is not replacing next property value({Level:u}).它总是期望第一个字段值为“+”值,如果该字段不存在意味着它不会替换下一个属性值({Level:u})。

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

If first field value other than "+" value, output as follows: {"time":"test ","severity":"{Level:u}","machine":"xxxxxx", "x-Correlation-ID":"f6133a7e-ea4f-4bde-8200-798d5346d3ce"}如果第一个字段值不是“+”值,则 output 如下:{“time”:“test”,“severity”:“{Level:u}”,“machine”:“xxxxxx”,“x-Correlation-ID ":"f6133a7e-ea4f-4bde-8200-798d5346d3ce"}

RollingFileAlternate sink used to log WriteTo.Async(w => w.RollingFileAlternate(logFilePath.ToString(), outputTemplate: logOutputTemplate, fileSizeLimitBytes: rollingFilesSize, retainedFileCountLimit: null)) RollingFileAlternate sink 用于记录 WriteTo.Async(w => w.RollingFileAlternate(logFilePath.ToString(), outputTemplate: logOutputTemplate, fileSizeLimitBytes: rollingFilesSize, reservedFileCountLimit: null))

how to remove first property with out effecting other output template properties.如何在不影响其他 output 模板属性的情况下删除第一个属性。

You're probably going to have to implement a custom ITextFormatter that implements the logic that you need, in order to create the corresponding output you want.您可能必须实现一个自定义ITextFormatter来实现您需要的逻辑,以便创建您想要的相应 output。

You can see how the the default ones ( CompactJsonFormatter.cs and RenderedCompactJsonFormatter.cs ) are implemented, and adapt the code to work how you need it.您可以查看默认的( CompactJsonFormatter.csRenderedCompactJsonFormatter.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM