Code inspection: The logging message template should not vary between calls to a logging method
This inspection highlights cases where a logging message template varies across multiple calls to logging methods, specifically those annotated with the StructuredMessageTemplate attribute from JetBrains.Annotations
. This can lead to inefficient logging and potentially incorrect log processing in structured logging systems.
The inspection is similar to CA2254 in Visual Studio, but it provides a quick-fix, which rewrites the interpolation-based logging message (e.g., logger.Log($"logging with {arguments} interpolation")
) into a structurally logged format (e.g., logger.Log("logging with {arguments} interpolation", arguments)
) as suggested in Microsoft guidelines.
The aim is to ensure that logging frameworks using structured logging recognize the provided strings as consistent and formatted message templates. Dynamic values or variables should always be logged as separate arguments rather than being interpolated into the message template itself.
Following these practices aligns with Microsoft's recommendations for structured logging. For more information about high-performance structured logging practices, refer to High-performance logging in .NET.