.NET by Patrik

Logging Lists and Arrays So Humans Can Read Them

When collections are logged directly, many logging systems only display the type name instead of the actual values. This makes it hard to understand what data was processed.

Converting the collection into a readable string solves this problem:

string[] values = { "One", "Two", "Three" };

var text = string.Join(", ", values);

logger.LogInformation("Values=[{Values}]", text);

To give additional context, the number of elements can also be logged:

logger.LogInformation(
    "Count={Count}, Values=[{Values}]",
    values.Length,
    string.Join(", ", values));

Readable output improves troubleshooting and reduces the need to reproduce issues locally.

logging
arrays
strings
clarity
csharp

Comments