-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S2629: Add C# and VbNet (#2509)
Add csharp to rule S2629
- Loading branch information
1 parent
80476f3
commit 0b9652d
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"title": "Logging templates should be constant", | ||
"quickfix": "infeasible" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |