3

"Microsoft.Extensions.Logging.LoggerExtensions.LogError" is throwing exception

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Code:

string SomeConstant = "Constant Value";
try
{
    //Some Code
}
catch (Exception ex)
{
    logger.LogError(ex, ex.Message + " {ValueOne} {ValueTwo} {ValueThree} ", SomeConstant, string.Empty, string.Empty);
        return false;
}

All the required parameters for the LogError Method are passed during the function invocation.

Assembly
Microsoft.Extensions.Logging.Abstractions
Version
2.2.0.0

Thanks in advance!

Shreyansh Deb
  • 53
  • 1
  • 1
  • 6
  • What is the reason of invoking LogError method with these additional parameters `, SomeConstant, string.Empty, string.Empty`? The `LogError` method parameter is expecting args `LogError(this ILogger logger, Exception exception, string message, params object[] args);` and those will be applied to pass parametrized message. But in your case, string is not parameterized – user1672994 May 25 '21 at 07:58
  • We require three additional information (related to the business requirements) to be logged, hence the additional parameter. – Shreyansh Deb May 25 '21 at 08:02
  • 1
    The add those as parametized string like `logger.LogError(ex, $" Message: ex.Message , Another String: Some string, param1 : {0}, param2 : {1}, param3: {3}", SomeConstant, string.Empty, string.Empty)`. Change the parameter name accordingly. You can also use structure logging for those additional parameters. – user1672994 May 25 '21 at 08:05
  • Any specific reason for doing this? Passing them as comma separated list of arguments works as well. Sample Code: https://ide.geeksforgeeks.org/CTpt4bs3vB Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params – Shreyansh Deb May 25 '21 at 08:14
  • 1
    The logger method is expecting that any of the additional arguments passed to it have corresponding format "holes" in the **message template**. When you pass arguments and there aren't any `{Properties}` the logger fails since it can't find any of the holes with an index corresponding to the added parameter's index in the array. Either add properties into the message template, or add your properties as logger scope data (or whatever your underlying logging framework--if not MS--uses to add extra info, ex. Serilog's Log Context) – pinkfloydx33 May 25 '21 at 08:18
  • 1
    And the specific reason is because MS.Extensions.Logging was designed to be generic, while also supporting structured Logging. So values passed in the arguments array must have a corresponding property in the message template. If you're not using structured Logging (Ex. Serilog), the logging framework ultimately does something *very close* to a `string.Format` call. Without property holes in the message, it has nowhere to actually stick the added information, and thus the exception – pinkfloydx33 May 25 '21 at 08:26
  • I totally missed it. That must be the issue. I'll update the code and monitor. Thanks @pinkfloydx33 – Shreyansh Deb May 25 '21 at 10:57
  • I the code checked again. We have passed the Message template in message parameter. Updating the question. Sorry my bad! – Shreyansh Deb May 25 '21 at 11:13
  • Try not naming the holes with numeric values. Call them `{Something}`, `{AnotherThing}`, and `{Other}` for example, just to see if it has to do with using numbers for the properties name – pinkfloydx33 May 25 '21 at 12:47

1 Answers1

2

Below seems work fine on my side:

    string SomeConstant = "Constant Value";
    try
    {
        int x = 1; int y = 0;
        int a = x / y;
        //Some Code
    }
    catch (Exception ex)
    {
        log.LogInformation("!!!!!!!!!!!!!!!!");
        log.LogError(ex,@"{0} {1} {2}",SomeConstant, string.Empty, string.Empty);
        log.LogInformation("!!!!!!!!!!!!!!!!");
    }
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27