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 code block will be counted, and any
logging statements within nested blocks will count towards their own. For example consider the snippet below:
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");
}
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: