Chapter 8 – Writing Structured and Correlated Logs
- The code uses string interpolation instead of semantic logging. A log message is formatted right away, so the
ILogger.Log
method is called underneath with the"hello world: 43, bar"
string, without any indication that there are two arguments with specific names and values.
If the Information
level is disabled, string interpolation happens anyway, serializing all arguments and calculating just the message to be dropped.
This code should be changed to logger.LogInformation("hello world: {foo}, {bar}", 42, "bar")
.
- We need to make sure that the usage report is built using log record properties that don’t change:
- A log message would change a lot when new arguments are added or code is refactored.
- The logging category is usually based on a namespace, which might change during refactoring. We can consider passing categories explicitly as strings instead of a generic type parameter...