![](/img/trans.png)
[英]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.