Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set Serilog formatter variables via appsettings.json?

I have the following appsettings.json configuration:

{
    "Serilog": {
        "Using": [],
        "MinimumLevel": {
            "Default": "Information",
            "Override": {
                "Microsoft": "Warning",
                "System": "Warning"
            }
        },
        "Enrich": [ "FromLogContext", "WithMachineName" ],
        "WriteTo": [
            {
                "Name": "File",
                "Args": {
                    "path": "C:\\Logs\\log.json",
                    "formatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch"
                }
            }
        ]
    }
}

What I am trying to configure in the above appsettings.json file would be represented in C# as something like this:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .WriteTo.File(new ElasticsearchJsonFormatter(inlineFields:true, renderMessageTemplate: false), @"C:\logs\log.json")
    .CreateLogger();

I need to set "inlineFields" equal to true and "renderMessageTemplate" equal to false as overrides in my appsettings.json file on the ElasticsearchJsonFormatter instance. Is there a way to do that in the appsettings.json file so that I can keep my configuration out of C#?

like image 270
Rob Packwood Avatar asked Mar 17 '20 14:03

Rob Packwood


People also ask

How to integrate serilog logger into an ASP NET Core Application?

Integrating Serilog logger into an ASP.NET Core application is very simple. You just need to follow the following steps: Configure Serilog as a Logging provider in the .NET Core host or, register Serilog with the Logger service during startup

What is the best jsonformatter for serilog?

Serilog’s built-in JsonFormatter implements this, but to my eyes, its output is awkward and verbose. The newer CompactJsonFormatter and RenderedCompactJsonFormatter in Serilog.Formatting.Compact produce cleaner JSON, and that format is supported by other tools in the Serilog ecosystem and beyond, so they’re a better starting point.

How to use text formatting instead of JSON format for log messages?

2) If we want to use text formatting instead of JSON formatting, the log message can be formatted using “outputTemplate” section inside file section like the following :

How to configure serilog logger configuration to output to console?

Configuring Serilog to output to the console is literally a one liner in Program.cs: Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); Customize Log message format. Each logger has a default template for the message that is to be logged.


1 Answers

I asked this question on GitHub: https://github.com/serilog/serilog-sinks-file/issues/138

No way :(

like image 169
xneg Avatar answered Oct 20 '22 00:10

xneg