Srini
Srini

Reputation: 41

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.

Upvotes: 4

Views: 1983

Answers (1)

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27868

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.csand 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();

Upvotes: 2

Related Questions