我正在.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缓存模板或类似的东西,第二种方法会更有效。
第一个看起来更易读,但是模板包含实际的log语句。
答案 0 :(得分:2)
第二个(使用消息模板)效率更高。
在第一个示例中,即使该级别的日志记录为“关闭”,消息也始终被格式化。在第二个示例中,只有在实际需要时才进行格式化。
使用df5 = df4 \
.groupby("arrival_date", "store_id", "warehouse_id", "class_id", "cloth_id") \
.agg(F.sum("quantity2").alias("want")) \
.withColumnRenamed("arrival_date", "Date")
df5.orderBy('Date').show()
+----------+--------+------------+--------+--------+----+
| Date|store_id|warehouse_id|class_id|cloth_id|want|
+----------+--------+------------+--------+--------+----+
|2020-08-04| 110| 1| 11010| M_1| 10|
|2020-08-06| 111| 1| 11010| M_2| 5|
|2020-08-06| 110| 1| 11010| M_1| 1|
|2020-08-07| 110| 1| 11010| M_1| 3|
+----------+--------+------------+--------+--------+----+
(字符串插值)也会阻碍Serilog的内部模板缓存,从而进一步降低性能。
https://nblumhardt.com/2016/07/event-types-structured-logging-concepts-in-net-4/对模板为何有用的问题进行了进一步的讨论。