简体   繁体   中英

Serilog custom outputTemplate

I was looking for custom output template format for logging

sample output template: "{\"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}).

For above template 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"}

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

how to remove first property with out effecting other output template properties.

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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