这种 C# 字符串格式的性质是什么? [英] What is the nature of this C# string formatting?

查看:24
本文介绍了这种 C# 字符串格式的性质是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容摘自 此 Microsoft 文档.

_logger.LogInformation("Queued Background Task {Guid} is running. " +
                    "{DelayLoop}/3", guid, delayLoop);

这种字符串格式是什么?换句话说,它叫什么,它在哪里记录?是 .NET 还是需要第三方库?

What is this type of string formatting? In other words what is it called and where is it documented? Is it .NET or does it require a third party library?

推荐答案

这是 ASP.NET Core 日志记录 - 或者更一般地说,通过 Microsoft.Extensions.Logging.Abstractions 进行日志记录.

It's ASP.NET Core logging - or more generally, logging via Microsoft.Extensions.Logging.Abstractions.

虽然它看起来有点像 C# 内插字符串文字,但消息实际上是由日志基础结构格式化的.该字符串是 消息模板.消息模板中指定的名称完全独立于后面提供的表达式 - 但占位符的顺序应该是所提供值的顺序.

Although it looks a bit like C# interpolated string literals, the message is actually formatted by the logging infrastructure. The string is a message template. The names specified in the message template are entirely independent of the expressions provided later - but the order of the placeholders is expected to be the order of the values provided.

这允许日志记录提供提取键/值对来执行结构化日志记录...例如,您可以以这样的 JSON 格式结束日志条目:

This allows logging provides to extract key/value pairs to perform structural logging... so for example you could end up with a log entry in JSON like this:

{
  "message": "Queued Background Task 1234-5678[...] is running. 100/3",
  "properties": {
    "Guid": "1234-5678[...]",
    "DelayLoop": 100
  }
}

(这取决于日志记录提供程序.)

(It depends on the logging provider.)

这篇关于这种 C# 字符串格式的性质是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆