16

I'm trying to trace/log some information on a message dispatching process that I'm working on.

When you try to use an object of a concrete position from the array of arguments more than once, this throws an exception.

    private void LogRandomInfo(ILogger Log, string processName, string messageType)
    {
        try
        {
            Log.LogInformation("{0} : The event {0} has received the info {1}", processName, messageType);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I was expecting something similar as when we use the string.Format. In string.Format, you can print as many times the requested value.

Therefore string.Format("{0} {0} {0}", 1); will print 1 1 1, but Log.LogInformation("{0} {0} {0}", 1); will throw an exception of type Index was outside the bounds of the array.

Is there any easy explanation of why this happens?

The logging library used is Microsoft.Extensions.Logging

3
  • What logging library is this?
    – canton7
    Commented May 23, 2019 at 15:33
  • 3
    If this is the Microsoft.Extensions.Logging implementation, note the docs say the message is the "Format string of the log message in message template format. "
    – stuartd
    Commented May 23, 2019 at 15:41
  • 1
    The logging library is The Logging Library is Microsoft.Extensions.Logging I don't have the need but I was fixing some code and I found that someone forgot a double {0} on a line and I was wondering wy this is failing. Commented May 24, 2019 at 6:51

1 Answer 1

44

It looks like you're using Microsoft.Extensions.Logging?

Microsoft.Extensions.Logging uses structured logging. This means that the placeholders are not numbers which refer to the different args passed, but are instead names which are associated with each of the args.

From the docs:

Each log specifies a message template. The message template can contain placeholders for which arguments are provided. Use names for the placeholders, not numbers.
...

_logger.LogInformation(LoggingEvents.GetItem, "Getting item {ID}", id);

...

The order of placeholders, not their names, determines which parameters are used to provide their values. In the following code, notice that the parameter names are out of sequence in the message template:

string p1 = "parm1";
string p2 = "parm2";
_logger.LogInformation("Parameter values: {p2}, {p1}", p1, p2);

The logging framework works this way so that logging providers can implement semantic logging, also known as structured logging. The arguments themselves are passed to the logging system, not just the formatted message template. This information enables logging providers to store the parameter values as fields. For example, suppose logger method calls look like this:

_logger.LogInformation("Getting item {ID} at {RequestTime}", id, DateTime.Now);

If you're sending the logs to Azure Table Storage, each Azure Table entity can have ID and RequestTime properties, which simplifies queries on log data. A query can find all logs within a particular RequestTime range without parsing the time out of the text message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.