【发布时间】:2016-11-07 20:39:51
【问题描述】:
我在 c# 应用程序中有以下 log4net 语句:
log.Info(CultureInfo.InvariantCulture, m => m(notice));
字符串内容为:
notice = "Checking: 645: Bp $B!!:{4V7r;K Bp $B$D$^$M$5$S (B <xxx@xxxxxx. Co. Jp> (B <xxxxx@xxxxxxx.Com>)"
导致此异常:
[Common.Logging.Factory.AbstractLogger+FormatMessageCallbackFormattedMessage]System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。 在 System.Text.StringBuilder.AppendFormat(IFormatProvider 提供程序,字符串格式,对象 [] 参数) 在 System.String.Format(IFormatProvider 提供程序,字符串格式,对象 [] 参数) 在 Common.Logging.Factory.AbstractLogger.FormatMessageCallbackFormattedMessage.FormatMessage(String format, Object[] args)
如果您注意到字符串(在这种情况下完全是垃圾)中有一个括号“{”。我相当确定这是导致异常的原因。我能做些什么来避免这种情况?以某种方式转义字符串?
这是一个相当无害的异常,只是它显示在日志文件中并且会分散注意力。
【问题讨论】:
-
我认为您最好将其标记为 Common.Logging
-
您使用的是什么 log4net?
log.Info(CultureInfo.InvariantCulture, m => m(notice));甚至不会编译为log.Info的第二个参数是Exception而不是表达式 -
您可以通过将
{和}加倍以在String.Format中使用它们。所以{{和}}。或者,如果您不想,请不要使用 String.Format。 -
@sqmoore,你说得很对,'common.logging',这将澄清其他 cmets 的一些困惑。
-
@HansKesting - { 括号来自字符串数据,我正在尝试处理,我想要一个比在输出前按摩字符串更理想的选择。另外,我自己没有使用 string.Format。
标签: c# log4net string.format common.logging