diff --git a/rules/S2629/csharp/metadata.json b/rules/S2629/csharp/metadata.json new file mode 100644 index 00000000000..2391e038bc2 --- /dev/null +++ b/rules/S2629/csharp/metadata.json @@ -0,0 +1,4 @@ +{ + "title": "Logging templates should be constant", + "quickfix": "infeasible" +} \ No newline at end of file diff --git a/rules/S2629/csharp/rule.adoc b/rules/S2629/csharp/rule.adoc new file mode 100644 index 00000000000..b4dec55f94e --- /dev/null +++ b/rules/S2629/csharp/rule.adoc @@ -0,0 +1,43 @@ +== 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: + +* https://www.nuget.org/packages/Microsoft.Extensions.Logging[Microsoft.Extensions.Logging] +* https://www.nuget.org/packages/Castle.Core[Castle.Core] +* https://www.nuget.org/packages/log4net[log4net] +* https://www.nuget.org/packages/Serilog[Serilog] +* https://www.nuget.org/packages/NLog[Nlog] + +=== Code examples + +==== Noncompliant code example + +[source,csharp,diff-id=1,diff-type=noncompliant] +---- +public void Method(ILogger logger, bool parameter) +{ + logger.DebugFormat($"The value of the parameter is: {parameter}."); +} +---- + +==== Compliant solution + +[source,csharp,diff-id=1,diff-type=compliant] +---- +public void Method(ILogger logger, bool parameter) +{ + logger.DebugFormat("The value of the parameter is: {Parameter}.", parameter); +} +---- + +== Resources + +=== Documentation + +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.interpolatedstringhandlerattribute[InterpolatedStringHandlerArgumentAttribute] \ No newline at end of file diff --git a/rules/S2629/metadata.json b/rules/S2629/metadata.json index 2c63c085104..974656d7e10 100644 --- a/rules/S2629/metadata.json +++ b/rules/S2629/metadata.json @@ -1,2 +1,29 @@ { + "title": "\"Preconditions\" and logging arguments should not require evaluation", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "performance", + "logging" + ], + "extra": { + "replacementRules": [ + + ], + "legacyKeys": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-2629", + "sqKey": "S2629", + "scope": "Main", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "unknown" }