Prasad Kanaparthi
Prasad Kanaparthi

Reputation: 6563

How to configure SeriLog ExpressionTemplate in AppSettings.Json

I'm trying to implement Serilog Expression Template to Log our application Logs in Json format in a FILE (not in Console). I'm unable to find the way to configure the template in AppSettings.json.

Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new ExpressionTemplate(
    "{{ TimeStamp: @t, Level: @l, Message: @m }}", theme: TemplateTheme.Code))
.CreateLogger();

Upvotes: 1

Views: 1879

Answers (2)

Cellcon
Cellcon

Reputation: 1295

You can do it like this, in a WriteTo section in the AppSettings.json.

"WriteTo": [
    {
        "Name": "File",
        "Args": {
            "Path": "log.txt",
            "Formatter": {
                "Type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
                "Template": "[{@t:dd.MM.yyyy HH:mm:ss}\n"
            }
        }
    }
]

Upvotes: 2

HaroldMorgan
HaroldMorgan

Reputation: 174

There probably isn't a way, but you can try add your logging template as a custom config parameter using IOptions interface and configuration builder

Upvotes: 1

Related Questions