Search code examples
c#string-interpolationiloggerstructured-logging

What method is called during Ilogger conversion of structured logging arguments to string?


I have an object that I want to pass to logging (default built-in logging to console, no libraries used) as argument like this:

logger.LogDebug("Executing MongoDB command: {Command}", command);

What I was expecting is that the result will be a call to command.ToString(), but the result of this call differs from the actual ToString() call.

P.S. The object in question is BsonDocument from MongoDB drivers if this has any importance.

P.P.S. I have compared to the interpolated string result as well and it is different (it seems it returns ToString() as I expected):

logger.LogDebug($"Executing MongoDB command: {command}");

Solution

  • The default formatter will format an IEnumerable object into a comma separated string, you can find the source code here:

    https://source.dot.net/#Microsoft.Extensions.Logging.Abstractions/LogValuesFormatter.cs,229