All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S2629.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

Logging arguments should not require evaluation in order to avoid unnecessary performance overhead. When passing concatenated strings or string interpolations directly into a logging method, the evaluation of these expressions occurs every time the logging method is called, regardless of the log level. This can lead to inefficient code execution and increased resource consumption.

Instead, it is recommended to use the overload of the logger that accepts a log format and its arguments as separate parameters. By separating the log format from the arguments, the evaluation of expressions can be deferred until it is necessary, based on the log level. This approach improves performance by reducing unnecessary evaluations and ensures that logging statements are only evaluated when needed.

Furthermore, using a constant log format enhances observability and facilitates searchability in log aggregation and monitoring software.

The rule covers the following logging frameworks:

How to fix it

Use an overload that takes the log format and the parameters as separate arguments. The log format should be a constant string.

Code examples

Noncompliant code example

logger.DebugFormat($"The value of the parameter is: {parameter}.");

Compliant solution

logger.DebugFormat("The value of the parameter is: {Parameter}.", parameter);

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy