带花括号的 String.Format
我们的低级日志库必须处理发送给它的各种日志消息。
其中一些消息包含大括号(作为文本的一部分),一些消息包含要使用 String.Format 格式化为字符串一部分的参数。
例如,此字符串可以是 Logger 类的输入:
“Parameter: {Hostname } 值:{0}” 发送正确的变量以供格式化程序使用。
为了正确地做到这一点,我必须转义不属于格式的花括号(通过将它们加倍)。
我想过使用正则表达式来完成此操作,但这并不像看起来那么简单,因为我不知道如何在大括号内匹配这些字符串(String.Format 不将其用于格式化目的的字符串)。
另一个问题是 Logger 类应该尽可能提高性能效率,开始处理正则表达式作为其操作的一部分可能会影响性能。
是否有任何适当且已知的最佳实践?
Our low level logging library has to cope with all sorts of log messages sent to it.
Some of these messages include curly braces (as part of the text), and some contain parameters to be formatted as part of the string using String.Format
For example, this string could be an input to the Logger class:
"Parameter: {Hostname} Value: {0}"
With the correct variable sent to use for the formatter.
In order to properly do it, i must escape the curly braces that are not part of the formatting (by doubling them up).
I thought of doing it using Regex, however this is not as simple as it may seem, since i have no idea how to match these strings inside a curly braces (ones that are NOT used by String.Format for formatting purposes).
Another issue is that the Logger class should be as performance efficient as possible, starting to handle regular expressions as part of its operation may hinder performance.
Is there any proper and known best practice for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会将所有花括号加倍,然后查找用
{{\d+}}
之类的正则表达式替换它们,以便它们恢复到原始格式 - {{0}} => {0} -- 在您的字符串中。因此,对于每一行,我都会这样做
,这是对原始问题的技术答案,但@Anders Abel 是完全正确的。值得再次考虑设计......
I would double all the curly braces and then I would look for those to be replaced with a regex like
{{\d+}}
so that they came back to their original format -- {{0}} => {0} -- in your string.So for each line I would do sth like this
So that's a technical answer to the original question but @Anders Abel is perfectly right. It would be worth considering the design again...
我认为您应该查看您的记录器界面。与
Console.WriteLine
的工作方式进行比较:Console.WriteLine(String)
准确输出给定的字符串,没有格式,{ 和 } 没有什么特别之处。Console.WriteLine(String, Object[])
使用格式输出。 { 和 } 是调用者必须转义为 {{ 和 }} 的特殊字符,我认为这是有缺陷的设计,必须区分代码中出现的不同花括号以找出其含义。将输出中应发生的转义 { 的负担置于 {{ 中。
I think that you should look into your loggers interface. Compare with how
Console.WriteLine
works:Console.WriteLine(String)
outputs exactly the string given, no formatting, nothing special with { and }.Console.WriteLine(String, Object[])
outputs using formatting. { and } are special characters that the caller must escape to {{ and }}I think it's flawed design having to differentiate between different curly brace occurences in the code to find out what as meant. Lay the burden of escaping { that should occur in the output into {{.
仅用一个正则表达式即可完成此操作:
输出:
这当然仅在不存在任何包含数字但仍应使用
{{ }}
转义的参数时有效Doing it in just one regex:
Outputs:
This of course only works as long as there aren't any parameters that contain numbers but should still be escaped with
{{ }}
允许调用者拥有格式化字符串并处理格式化说明符,例如
Logger.Log("{0:dd/mm/yyy} {0:hh:mm:ss} {hostname} 发生了一些错误 {1:x4} { 123Component}!", DateTime.UtcNow, 257)
您需要一个正则表达式,例如:
此输出:
但重申一下,我同意 Anders Abel 的观点,您应该重新设计为避免需要日志库来执行此操作。
To allow the caller to have formatted strings and cope with formitting specifiers e.g.
Logger.Log("{0:dd/mm/yyy} {0:hh:mm:ss} {hostname} Some error {1:x4} happened on {123Component}!", DateTime.UtcNow, 257)
You'd need a regex like:
This outputs:
But to reiterate, I'd agree with Anders Abel that you ought to redesign to avoid the need for the log library to do this.