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

3

7

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#?

Lilia answered 17/3, 2020 at 14:37 Comment(0)
S
1

As said in the link given in @xneg's response, it wasn't possible back in the days, but as @leftiness said, it's possible since v3.3.0!

I successfully used it with these appsettings:

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": {
            "type": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch",
            "renderMessage": true
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
Sicklebill answered 29/5 at 20:37 Comment(0)
L
4

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

No way :(

Lunulate answered 30/3, 2020 at 18:10 Comment(0)
P
3

They added this feature in Serilog.Settings.Configuration -v 3.3.0-dev-00291.

Serilog__WriteTo__Console__Name=Console
Serilog__WriteTo__Console__Args__formatter__type="Serilog.Templates.ExpressionTemplate, Serilog.Expressions"
Serilog__WriteTo__Console__Args__formatter__template="[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}"
[22:38:40 INF My.Foo.FooClient] HELLO WORLD

links

Purview answered 14/9, 2021 at 22:51 Comment(1)
And it has stable released just now!Algarroba
S
1

As said in the link given in @xneg's response, it wasn't possible back in the days, but as @leftiness said, it's possible since v3.3.0!

I successfully used it with these appsettings:

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": {
            "type": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch",
            "renderMessage": true
          }
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
Sicklebill answered 29/5 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.