diff --git a/rules/S6664/csharp/compliant.adoc b/rules/S6664/csharp/compliant.adoc new file mode 100644 index 0000000000..60de16bd22 --- /dev/null +++ b/rules/S6664/csharp/compliant.adoc @@ -0,0 +1,19 @@ + +[source,csharp,diff-id=1,diff-type=compliant] +---- +void MyMethod(List 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"); +} +---- \ No newline at end of file diff --git a/rules/S6664/csharp/example.adoc b/rules/S6664/csharp/example.adoc new file mode 100644 index 0000000000..2b86de7cf3 --- /dev/null +++ b/rules/S6664/csharp/example.adoc @@ -0,0 +1,15 @@ + +[source,csharp] +---- +void MyMethod(List 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"); +} +---- \ No newline at end of file diff --git a/rules/S6664/csharp/metadata.json b/rules/S6664/csharp/metadata.json new file mode 100644 index 0000000000..7a73a41bfd --- /dev/null +++ b/rules/S6664/csharp/metadata.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/rules/S6664/csharp/noncompliant.adoc b/rules/S6664/csharp/noncompliant.adoc new file mode 100644 index 0000000000..f0b3d775fb --- /dev/null +++ b/rules/S6664/csharp/noncompliant.adoc @@ -0,0 +1,20 @@ + +[source,csharp,diff-id=1,diff-type=noncompliant] +---- +void MyMethod(List 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"); +} +---- \ No newline at end of file diff --git a/rules/S6664/csharp/rule.adoc b/rules/S6664/csharp/rule.adoc new file mode 100644 index 0000000000..d9135d1836 --- /dev/null +++ b/rules/S6664/csharp/rule.adoc @@ -0,0 +1,3 @@ +:language: csharp + +include::../rule.adoc[] \ No newline at end of file diff --git a/rules/S6664/metadata.json b/rules/S6664/metadata.json new file mode 100644 index 0000000000..9a652486a8 --- /dev/null +++ b/rules/S6664/metadata.json @@ -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" +} diff --git a/rules/S6664/rspecator.adoc b/rules/S6664/rspecator.adoc new file mode 100644 index 0000000000..974fc29fc8 --- /dev/null +++ b/rules/S6664/rspecator.adoc @@ -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[] \ No newline at end of file diff --git a/rules/S6664/rule.adoc b/rules/S6664/rule.adoc new file mode 100644 index 0000000000..f220126989 --- /dev/null +++ b/rules/S6664/rule.adoc @@ -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[] \ No newline at end of file