elasticsearch serilog在序列化时展平对象属性

gorkyyrv  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(0)|浏览(353)

我正在使用.net核心应用程序发送(记录)一些数据 Serilog 以及 Serilog.Sinks.Elasticsearch . 假设我有一个简单的类:

public class ElkData
{
    public DateTime EventTime { get; set; }
    public String TransactionType { get; set; }
    public String Status { get; set; }
}

使用 @ 操作员:

log.Information("{@ElkData}", elkData);

属性存储在嵌套在 ElkData 字段:

{
    "_index" : "serilog_data",
    "_type" : "_doc",
    "_id" : "0_9cb531b8-049b-4d07-98f9-b2db1fb65613",
    "_score" : 1.0,
    "_source" : {
      "insertTime" : "2020-09-17T11:20:25.386101Z",
      "ElkData" : {
        "EventTime" : "2020-09-17T13:20:20.5911226+02:00",
        "Status" : "0",
        "TransactionType" : "Test",
        "_typeTag" : "ElkData"
      },
      "Type" : "ElkData",
      "@timestamp" : "2020-09-17T13:20:20.5925136+02:00",
      "level" : "Information",
      "message" : """ElkData {EventTime: 09/17/2020 13:20:20, TransactionType: "Test",  Status: "0" }""",
      "messageTemplate" : "{@ElkData}"
    }
  }

我希望它们位于根元素中(在elkdata对象之外-展平)。我可以使用:

log.Information("{eventTime}{transactionType}{status}", elkData.EventTime, elkData.TransactionType, elkData.Status);

这给了我想要的:

{
    "_index" : "serilog_data",
    "_type" : "_doc",
    "_id" : "0_27076c7a-dd7b-4092-9b7f-74563edbb004",
    "_score" : 1.0,
    "_source" : {
      "level" : "Information",
      "insertLagInSeconds" : 4,
      "message" : "09/17/2020 13:21:24\"Test\"\"0\"",
      "transactionType" : "Test",
      "insertTime" : "2020-09-17T11:21:29.738153Z",
      "Type" : "ElkData",
      "@timestamp" : "2020-09-17T13:21:24.8869414+02:00",
      "eventTime" : "2020-09-17T13:21:24.8855001+02:00",
      "messageTemplate" : "{eventTime}{transactionType}{status}",
      "status" : "0"
    }
  }

有没有比在消息模板中写入所有元素的属性并将所有属性值放在后面更好的方法呢?这可以通过指示/重写serilog格式化程序来完成吗?
提前谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题