Skip to content

Logging correctly

When logging in C# the message is not just a string, but actually a message template and should not use interpolation/formatting/concatenations and explicit values, for example:

// Incorrect approach
_logger.LogInformation($"The value is {value} after {attempts} attempts.");

This approach loses all the parameters of the message, making it impossible to filter the errors and allocates strings that will eventually need to be garbage collected.

For the above example, we should really use the template approach with the values as parameters:

// Correct approach
_logger.LogInformation("The value is {value} after {attempts} attempts.", value, attempts);

This is faster and allows filtering, not only for basic code but also with loggers like SEQ.