Create rule S6664: too many logging calls within a code block (#2494)
This commit is contained in:
parent
e858b2b862
commit
460fc9c64c
19
rules/S6664/csharp/compliant.adoc
Normal file
19
rules/S6664/csharp/compliant.adoc
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
void MyMethod(List<MyObject> items)
|
||||
{
|
||||
logger.Debug("The operation started");
|
||||
foreach(var item in items)
|
||||
{
|
||||
logger.Information($"Evaluating {item.Name}");
|
||||
var result = Evaluate(item);
|
||||
if (item.Name is string.Empty)
|
||||
{
|
||||
logger.Error("Invalid item name");
|
||||
}
|
||||
logger.Information($"End item evaluation with result: {result}");
|
||||
}
|
||||
logger.Debug("The operation ended");
|
||||
}
|
||||
----
|
15
rules/S6664/csharp/example.adoc
Normal file
15
rules/S6664/csharp/example.adoc
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
[source,csharp]
|
||||
----
|
||||
void MyMethod(List<MyObject> items)
|
||||
{
|
||||
logger.Debug("The operation started");
|
||||
foreach(var item in items)
|
||||
{
|
||||
logger.Debug($"Evaluating {item.Name}");
|
||||
var result = Evaluate(item);
|
||||
logger.Debug($"Evaluating resulted in {result}");
|
||||
}
|
||||
logger.Debug("The operation ended");
|
||||
}
|
||||
----
|
2
rules/S6664/csharp/metadata.json
Normal file
2
rules/S6664/csharp/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
20
rules/S6664/csharp/noncompliant.adoc
Normal file
20
rules/S6664/csharp/noncompliant.adoc
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
void MyMethod(List<MyObject> items)
|
||||
{
|
||||
logger.Debug("The operation started");
|
||||
foreach(var item in items)
|
||||
{
|
||||
logger.Information($"Evaluating {item.Name}"); // Noncompliant
|
||||
var result = Evaluate(item);
|
||||
logger.Information($"Evaluating resulted in {result}"); // Secondary 1
|
||||
if (item.Name is string.Empty)
|
||||
{
|
||||
logger.Error("Invalid item name");
|
||||
}
|
||||
logger.Information("End item evaluation"); // Secondary 2
|
||||
}
|
||||
logger.Debug("The operation ended");
|
||||
}
|
||||
----
|
3
rules/S6664/csharp/rule.adoc
Normal file
3
rules/S6664/csharp/rule.adoc
Normal file
@ -0,0 +1,3 @@
|
||||
:language: csharp
|
||||
|
||||
include::../rule.adoc[]
|
22
rules/S6664/metadata.json
Normal file
22
rules/S6664/metadata.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"title": "The code block contains too many logging calls",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"logging"
|
||||
],
|
||||
"extra": {
|
||||
},
|
||||
"defaultSeverity": "Minor",
|
||||
"ruleSpecification": "RSPEC-6664",
|
||||
"sqKey": "S6664",
|
||||
"scope": "Main",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "infeasible"
|
||||
}
|
57
rules/S6664/rspecator.adoc
Normal file
57
rules/S6664/rspecator.adoc
Normal file
@ -0,0 +1,57 @@
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
=== Message
|
||||
|
||||
Reduce the XXX logging calls within this code block.
|
||||
|
||||
=== Highlighting
|
||||
|
||||
* Primary: 1st logging statement
|
||||
* Secondary 1: 2nd logging statement
|
||||
* Secondary 2: 3rd logging statement
|
||||
* Secondary 3: 4th logging statement
|
||||
...
|
||||
|
||||
=== Parameters
|
||||
|
||||
.debugThreshold
|
||||
****
|
||||
----
|
||||
4
|
||||
----
|
||||
The maximum number of DEBUG, TRACE and VERBOSE statements allowed in the same code block
|
||||
****
|
||||
|
||||
.informationThreshold
|
||||
****
|
||||
----
|
||||
2
|
||||
----
|
||||
The maximum number of INFORMATION statements allowed in the same code block
|
||||
****
|
||||
|
||||
.warningThreshold
|
||||
****
|
||||
----
|
||||
1
|
||||
----
|
||||
The maximum number of WARNING statements allowed in the same code block
|
||||
****
|
||||
|
||||
.errorThreshold
|
||||
****
|
||||
----
|
||||
1
|
||||
----
|
||||
The maximum number of ERROR and FATAL statements allowed in the same code block
|
||||
****
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
endif::env-github,rspecator-view[]
|
56
rules/S6664/rule.adoc
Normal file
56
rules/S6664/rule.adoc
Normal file
@ -0,0 +1,56 @@
|
||||
A code block should not contain too many logging statements of a specific level.
|
||||
|
||||
== Why is this an issue?
|
||||
|
||||
Excessive logging within a code block can lead to several problems:
|
||||
|
||||
* *Log file overload*: generating an overwhelming number of log entries can fill up disk space quickly (thus increasing the storage space cost) and make it challenging to identify important log events promptly.
|
||||
* *Performance degradation*: writing a large number of log statements can impact the performance of an application, especially when the logs are placed in frequently executed paths.
|
||||
* *Code readability and maintainability*: excessive logging can clutter the code and increase the code's complexity, making it difficult for developers to identify essential logic.
|
||||
|
||||
Only the logging statements that are directly within the https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/program-building-blocks#statements[code block] will be counted, and any logging statements within nested blocks will count towards their own. For example consider the snippet below:
|
||||
|
||||
include::{language}/example.adoc[]
|
||||
|
||||
The rule will count 2 logging statements that are within the method block (namely `logger.Debug("The operation started")` and `logger.Debug("The operation ended")`). Any statements within nested blocks, such as the `foreach` block will be counted separately.
|
||||
The rule considers the log level of the calls, as follows:
|
||||
|
||||
* *Debug*, *Trace* and *Verbose* logging level statements will count together and raise when the *_Debug threshold_* parameter is exceeded (default value: _4_);
|
||||
* *Information* logging level statements will raise when the *_Information threshold_* parameter is exceeded (default value: _2_);
|
||||
* *Warning* logging level statements will raise when the *_Warning threshold_* parameter is exceeded (default value: _1_);
|
||||
* *Error* and *Fatal* logging level statements will count together and raise when the *_Error threshold_* parameter is exceeded (default value: _1_);
|
||||
|
||||
The most popular logging frameworks are supported:
|
||||
|
||||
* Nuget package - https://www.nuget.org/packages/Microsoft.Extensions.Logging[Microsoft.Extensions.Logging]
|
||||
* Nuget package - https://www.nuget.org/packages/Serilog[Serilog]
|
||||
* Nuget package - https://www.nuget.org/packages/Castle.Core[Castle.Core]
|
||||
* Nuget package - https://www.nuget.org/packages/NLog[NLog]
|
||||
* Nuget package - https://www.nuget.org/packages/log4net[log4net]
|
||||
|
||||
== How to fix it
|
||||
|
||||
Reduce the number of specific logging level calls within the code block by identifying and selecting essential log statements with relevant information, necessary for understanding the flow of execution or diagnosing issues.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
With the default Information threshold parameter value 2:
|
||||
|
||||
include::{language}/noncompliant.adoc[]
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
With the default Information threshold parameter value 2:
|
||||
|
||||
include::{language}/compliant.adoc[]
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/program-building-blocks#statements[Code blocks]
|
||||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements[Exception-handling statements]
|
||||
|
||||
include::rspecator.adoc[]
|
Loading…
x
Reference in New Issue
Block a user