How to send Serilog data to Elasticsearch with fields How to send Serilog data to Elasticsearch with fields elasticsearch elasticsearch

How to send Serilog data to Elasticsearch with fields


I figured it out. I changed the construction to use the ElasticsearchJsonFormatter. Since the logger seemed to be able to parse the field name from the message, I switched to an object and passed in the properties instead:

        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);

That produced a much more readable output in 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    }  }