繁体   English   中英

在 .NET 中使用 Serilog 更高效的日志记录是什么?

[英]What is more efficient logging in Serilog in .NET?

我在 .NET 核心应用程序中使用 Serilog 进行日志记录。

我以两种方式登录,我不确定一种是否比另一种更有效。

  1. 使用 $ 前缀序列化属性
 _logger.LogInformation($"consumer:{_consumer.ConsumerName}. clientId:{_consumer.ClientId}." +
                                       $"max_handle_latency:{_maxHandleMessageLatencyInterval}." +
                                       $"total_consume_requests:{_numConsumingOperatonsInInterval}. topics:{topics}");
  1. 不使用 $ - 通过将参数传递给模板。
 _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 缓存模板或任何类似的东西,第二个会更有效。

第一个看起来更具可读性,但模板包含实际的日志语句。

第二个(使用消息模板)效率更高。

在第一个示例中,消息始终被格式化,即使该级别的日志记录为“关闭”。 在第二个示例中,仅在实际需要时才会进行格式化。

使用$ (字符串插值)也会阻碍 Serilog 的内部模板缓存,进一步降低性能。

https://nblumhardt.com/2016/07/event-types-structured-logging-concepts-in-net-4/进一步讨论了模板为何有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM