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[]