[英]Error Index (zero based) must be greater than or equal to zero and less than the size of the argument list
[英]Exception during StringFormat: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
我在现场很少见到这样的示例,但是所有人都在错误地将占位符命名为错误或参数和占位符的数量不同,我的日志行如下所示
Logger.InfoFormat("Successfully connected to outgoing queue for platform {0}. QueueManagerName = {1}, HostName = {2}, ChannelName = {3}, QueueName = {4}", Platform.ID, Platform.MqGatewayParams.QueueManagerName, Platform.MqGatewayParams.HostName, Platform.MqGatewayParams.ChannelName, Platform.MqGatewayParams.OutgoingQueueName);
InfoFormat方法:
public void InfoFormat(string className, string methodName, string format, object arg0, object arg1, object arg2)
{
_log4NetLogger.InfoFormat(GetMessageString(className, methodName, format), arg0, arg1, arg2);
}
在内部它调用GeMessageString
private string GetMessageString(string className, string methodName, object message)
{
return string.Format("[{0}::{1}] {2}", className ?? string.Empty, methodName ?? " ", message ?? " ");
}
有人能告诉我我在做什么错吗?
在InfoFormat
方法中,您可以:
_log4NetLogger.InfoFormat(GetMessageString(className, methodName, format), arg0, arg1, arg2);
这仅将3个格式参数传递给_log4NetLogger.InfoFormat
,但是您的格式字符串有5个。
您需要将params
用于可变长度参数列表,如下所示:
void FormatString(string format, params object[] args) {
String.Format(format, args)
}
(为较短的参数列表提供重载有一个小的好处-以String.Format
本身为例-因为不需要分配数组;但这是一个小的优化,除非经常使用这些函数。)
PS。 示例调用者似乎正在将格式字符串作为InfoFormat
的第一个参数InfoFormat
,但其实现似乎期望参数查找该字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.