rspec/rules/S2139/rule-dotnet.adoc
2024-03-08 17:24:42 +01:00

38 lines
1.5 KiB
Plaintext

== Why is this an issue?
When an exception is logged and rethrown, the upstream code may not be aware that the exception has already been logged.
As a result, the same exception gets logged multiple times, making it difficult to identify the root cause of the issue.
This can be particularly problematic in multi-threaded applications where messages from other threads can be interwoven with the repeated log entries.
=== Exceptions
This rule will not generate issues if, within the catch block, one of the following conditions are met:
* The logs generated within the catch block do not contain any references to the exception being caught.
* The exception being thrown from the catch block is not the same exception that is being caught.
== How to fix it
To address this issue, it is recommended to modify the code to log exceptions only when they are handled locally. In all other cases, simply rethrow the exception and allow the higher-level layers of the application to handle the logging and appropriate actions.
=== Code examples
==== Noncompliant code example
include::{language}/noncompliant.adoc[]
==== Compliant solution
include::{language}/compliant.adoc[]
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements[Exception-handling statements]
=== Articles & blog posts
* https://rolf-engelhard.de/2013/04/logging-anti-patterns-part-ii/[Rolf Engelhard - Logging anti-patterns]
include::rspecator.adoc[]