Ant*_*Ant 5 .net elasticsearch serilog
我是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);
Run Code Online (Sandbox Code Playgroud)
这将产生:
@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
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以使用ES(和Kibana)中的字段来搜索/可视化我们的数据?
我想到了。我将构造更改为使用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);
Run Code Online (Sandbox Code Playgroud)
这在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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3248 次 |
最近记录: |