Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add customized JSON Formmter in appsettings.json

Tags:

serilog

I created a customized JSON Formatter which extends ITextFormatter And try to add it to appsetting.json like this:

"WriteTo": [
 {
    "Name": "Console",
    "Args": {
        "formatter": "CustomizedJSONFormatter"
       // "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
    }
  } 
]

The default jsonFormatter works fine as shown in the commented line. But I will get an error if I want to add the customized one: InvalidCastException: Invalid cast from 'System.String' to 'Serilog.Formatting.ITextFormatter'.

Is there anyway to solve this?

Thanks!

like image 760
XueFan Wu Avatar asked Oct 24 '25 08:10

XueFan Wu


2 Answers

As suggested by @SLaks, the assembly-qualified type name is required:

MyNamespace.CustomizedJsonFormatter, MyAssembly
like image 164
Nicholas Blumhardt Avatar answered Oct 27 '25 12:10

Nicholas Blumhardt


It needs to be used like below:

"WriteTo": [
 {
    "Name": "Console",
    "Args": {
       "customFormatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch"
    }
  } 
]
like image 39
Manan Avatar answered Oct 27 '25 10:10

Manan