在appsettings.json中添加自定义的JSON Formmter

时间:2017-11-02 14:32:56

标签: serilog

我创建了一个自定义的JSON Formatter,它扩展了ITextFormatter 并尝试将其添加到appsetting.json,如下所示:

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

默认的jsonFormatter工作正常,如注释行所示。 但如果我想添加自定义的,我会收到错误: InvalidCastException:来自' System.String'的无效演员表: to' Serilog.Formatting.ITextFormatter'。

有没有解决这个问题?

谢谢!

4 个答案:

答案 0 :(得分:1)

如果它对其他人有帮助:在我的情况下,缺少以下 Nuget 包,在我安装它后,错误消失了:

<块引用>

Serilog.Formatting.Elasticsearch

答案 1 :(得分:0)

正如@SLaks所建议的那样,需要使用程序集限定的类型名称:

def speedLimit():
    designatedLimit = int(input("Please enter the designated speed limit: "))
    while designatedLimit not in range(20, 70):
        print("Invalid entry: please enter a valid speed limit (20-70)")
        designatedLimit = int(input("Please enter the designated speed limit: "))
    return designatedLimit

def vehicleSpeed(designatedLimit):
    driverSpeed = int(input("Please enter the vehicle's speed: "))
    while driverSpeed <= designatedLimit:
        print("You were not speeding!")
        driverSpeed = int(input("Please enter a valid vehicle speed: "))
        return driverSpeed

def violationCalculator(designatedLimit, driverSpeed):
    speedExcess = driverSpeed - designatedLimit
    return speedExcess

def main():
    welcomeSplash()
    designatedLimit = speedLimit()
    driverSpeed = vehicleSpeed(designatedLimit)
    speedExcess = violationCalculator(designatedLimit, driverSpeed)
    print("If you were travelling at", driverSpeed, "in a", designatedLimit, 
    "zone, then you were over the designated speed limit by", speedExcess)
main()

答案 2 :(得分:0)

它的用法如下:

"WriteTo": [
 {
    "Name": "Console",
    "Args": {
       "customFormatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch"
    }
  } 
]

答案 3 :(得分:0)

正如@Rodrigo Leite 所建议的,您确实需要引用 Nuget 包

<块引用>

Serilog.Formatting.Elasticsearch

但也需要在appsettings文件中更改formatter的路径:

"formatter": 
"Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter, Serilog.Formatting.Elasticsearch"