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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With