【问题标题】:How to send Serilog data to Elasticsearch with fields如何使用字段将 Serilog 数据发送到 Elasticsearch
【发布时间】:2016-07-11 18:55:26
【问题描述】:

我是 ES 和 Serilog 的新手,但我的搜索还没有产生这个答案。我试图弄清楚如何使用 Serilog 将数据发送到 Elasticsearch,如果数据包含字段(例如,如果它是具有公共属性的对象),则数据在 ES 中显示为具有这些属性字段。到目前为止,我已经尽可能使用 RenderedCompactJsonFormatter 和匿名类型来实现这个大部分(见下文),但这仍然会产生命名字段,其中字段中的数据是除了匿名类型声明的“新”部分:

            var log = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/test_srpostimes"))
                {
                    InlineFields = true,
                    IndexDecider = (@event,offset) => "test_elapsedtimes",
                    CustomFormatter = new RenderedCompactJsonFormatter()
                })
                .WriteTo.Console()
                .CreateLogger();
            var elapsedTime = new {Time = 64};
            var timeStamp = new {Timestamp = DateTime.Now};
            var transID = new {TransID = "551674"};

            log.Information("{timeStamp} {transID} {elapsedTime}", timeStamp, transID, elapsedTime);

这会产生:

@t:
    2016-07-11T18:45:35.0349343Z
@m:
    "{ Timestamp = 7/11/2016 2:45:35 PM }" "{ TransID = 551674 }" "{ Time = 64 }"
@i:
    b3ee2c05
timeStamp:
    { Timestamp = 7/11/2016 2:45:35 PM }
transID:
    { TransID = 551674 }
elapsedTime:
    { Time = 64 }
_id:
    AVXbR11WjgSgCs5HSlYY
_type:
    logevent
_index:
    test_srpostimes
_score:
    1

有没有更好的方法来做到这一点,以便我们的数据可以使用 ES(和 Kibana)中的字段进行搜索/可视化?

【问题讨论】:

  • 嗨蚂蚁,你见过:github.com/serilog/serilog-sinks-elasticsearch 吗?可能有助于完成这项任务。
  • 是的,我一直在使用您的出色工具 :) 但我想我要么错过了它解释如何做到这一点的地方,要么可能(更有可能?)我对 ES 和 Serilog 太陌生了否则会知道如何通过更多经验来完成此任务。我将立即更新问题,以展示我如何构建记录器和接收器以更好地澄清问题。

标签: .net elasticsearch serilog


【解决方案1】:

我想通了。我更改了构造以使用 ElasticsearchJsonFormatter。由于记录器似乎能够从消息中解析字段名称,因此我切换到一个对象并传入属性:

        var log = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/test_srpostimes"))
            {
                IndexDecider = (@event,offset) => "test_elapsedtimes",
                CustomFormatter = new ElasticsearchJsonFormatter()
            })
            .WriteTo.Console()
            .CreateLogger();

            var elapsedTimeMessage = new ElapsedTimeMessage(DateTime.Now.Millisecond);

            log.Information("{EventTime} {EventId} {ElapsedTime}", elapsedTimeMessage.EventTime, elapsedTimeMessage.EventId, elapsedTimeMessage.ElapsedTime);

这在 ES 中产生了更具可读性的输出:

  "_source": {
    "@timestamp": "2016-07-12T09:03:21.5804873-04:00",
    "level": "Information",
    "messageTemplate": "{EventTime} {EventId} {ElapsedTime}",
    "fields": {
      "EventTime": "2016-07-12T09:03:21.5754873-04:00",
      "EventId": "575",
      "ElapsedTime": 575
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多