Anamay
Anamay

Reputation: 71

Exception during StringFormat: Index (zero based) must be greater than or equal to zero and less than the size of the argument list

I have seen few examples like this on site but all are having errors with the place holders being named wrong or having differences in number of parameters and place holders , my logline is like below

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);

The InfoFormat method :

public void InfoFormat(string className, string methodName, string format, object arg0, object arg1, object arg2)
    {
        _log4NetLogger.InfoFormat(GetMessageString(className, methodName, format), arg0, arg1, arg2);
    }

and internally it calls GeMessageString

private string GetMessageString(string className, string methodName, object message)
    {
        return string.Format("[{0}::{1}] {2}", className ?? string.Empty, methodName ?? " ", message ?? " ");
    }

Can any body tell me what am i doing wrong here ?

Upvotes: 0

Views: 2183

Answers (1)

Richard
Richard

Reputation: 109110

In the InfoFormat method you have:

_log4NetLogger.InfoFormat(GetMessageString(className, methodName, format), arg0, arg1, arg2);

This is only passing 3 format arguments to _log4NetLogger.InfoFormat, but your format string has 5.

You need to make use of params for variable length parameter lists, like this:

void FormatString(string format, params object[] args) {
  String.Format(format, args)
}

(There is a small benefit of providing overloads for shorter parameter lists – use String.Format itself as an example – as the allocation of an array is not needed; but it is a small optimisation unless these functions are used a lot.)

PS. The sample caller seems to be passing a format string as the first argument of your InfoFormat, but its implementation appears to be expecting parameters to look up the string.

Upvotes: 2

Related Questions