【发布时间】:2020-09-30 09:57:09
【问题描述】:
我在 .NET 核心应用程序中使用 Serilog 进行日志记录。
我以两种方式登录,我不确定一种是否比另一种更有效。
- 使用$前缀序列化属性
_logger.LogInformation($"consumer:{_consumer.ConsumerName}. clientId:{_consumer.ClientId}." +
$"max_handle_latency:{_maxHandleMessageLatencyInterval}." +
$"total_consume_requests:{_numConsumingOperatonsInInterval}. topics:{topics}");
- 不使用 $ - 通过将参数传递给模板。
_logger.LogInformation("producer:{name}. total_produce_requests:{total_produce_requests}. max_latency:{max_latency}. intervalInMs:{interval_in_ms}. topics:{topics}",
_kafkaProducer.Name,
_numProduceOperationsInInterval,
_maxLatencyInInterval,
_producerIntervalInMs,
string.Join(",", topics));
这会导致这两个不同的日志语句。查看 MessageTemplate 和属性。
1.
{
"Timestamp": "2020-09-29T11:58:39.0343478-07:00",
"Level": "Information",
"MessageTemplate": "consumer:DotNetCoreReferenceApplication-myhost#consumer-7. clientId:DotNetCoreReferenceApplication-BL-9HQ76S2.max_handle_latency:35.total_consume_requests:1356. topics:bl-perf",
"Properties": {
"SourceContext": "MySvc.Messaging.EventsHub.MyConsSvc",
"ThreadId": 27,
"ActionId": "c1bbd73f-634b-44dc-aa3e-b2c09a097fd1",
"ActionName": "MySvc.Messaging.Api.Controllers.ConsumerConfigController.SetConsumerConfig (MySvc.Messaging.Api)",
"RequestId": "0HM34L5IO01I8:00000001",
"RequestPath": "/eventhub/consumer/setconfiguration",
"SpanId": "|6bb4afcf-4eafc985509764f0.",
"TraceId": "6bb4afcf-4eafc985509764f0",
"ParentId": "",
"ConnectionId": "0HM34L5IO01I8"
}
}
{
"Timestamp": "2020-09-29T12:00:39.0139328-07:00",
"Level": "Information",
"MessageTemplate": "producer:{name}. total_produce_requests:{total_produce_requests}. max_latency:{max_latency}. intervalInMs:{interval_in_ms}. topics:{topics}",
"Properties": {
"name": "DotNetCoreReferenceApplication-mydevbox#producer-6",
"total_produce_requests": 0,
"max_latency": 0,
"interval_in_ms": 60000,
"topics": "",
"SourceContext": "MySvc.Messaging.EventsHub.Producer.EventBusProducer",
"ThreadId": 8,
"ActionId": "c1bbd73f-634b-44dc-aa3e-b2c09a097fd1",
"ActionName": "MySvc.Messaging.Api.Controllers.ConsumerConfigController.SetConsumerConfig (MySvc.Messaging.Api)",
"RequestId": "0HM34L5IO01I8:00000001",
"RequestPath": "/eventshub/perf/csmr/config",
"SpanId": "|6bb4afcf-4eafc985509764f0.",
"TraceId": "6bb4afcf-4eafc985509764f0",
"ParentId": "",
"ConnectionId": "0HM34L5IO01I8"
}
}
虽然后者看起来不像第一个那样可读,但它似乎更有意义,因为您希望模板成为模板!所以我怀疑如果 Serilog 缓存模板或类似的东西,第二个会更有效。
虽然第一个看起来更易读,但模板包含实际的日志语句。
【问题讨论】: