Skip to content

Commit

Permalink
Create rule S2629: Add C# and VbNet (#2509)
Browse files Browse the repository at this point in the history
Add csharp to rule S2629
  • Loading branch information
github-actions[bot] authored Feb 22, 2024
1 parent 80476f3 commit 0b9652d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rules/S2629/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Logging templates should be constant",
"quickfix": "infeasible"
}
43 changes: 43 additions & 0 deletions rules/S2629/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -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]
27 changes: 27 additions & 0 deletions rules/S2629/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 0b9652d

Please sign in to comment.