-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
710031d
commit 334e99d
Showing
11 changed files
with
103 additions
and
35 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[source,csharp,diff-id=1,diff-type=compliant] | ||
---- | ||
try {} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex.Message); | ||
// Handle exception | ||
} | ||
---- | ||
|
||
or | ||
|
||
[source,csharp] | ||
---- | ||
try {} | ||
catch (Exception ex) | ||
{ | ||
// ... | ||
throw; | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[source,csharp,diff-id=1,diff-type=noncompliant] | ||
---- | ||
try {} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex.Message); | ||
throw; | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:language: csharp | ||
|
||
include::../rule-dotnet.adoc[] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,3 @@ | ||
include::../rule.adoc[] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
include::../message.adoc[] | ||
|
||
include::../highlighting.adoc[] | ||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
include::../comments-and-links.adoc[] | ||
|
||
endif::env-github,rspecator-view[] | ||
include::../rspecator.adoc[] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Either log this exception and handle it, or rethrow it with some contextual information. | ||
|
||
=== Highlighting | ||
|
||
* Primary: The exception caught | ||
* Secondary 1: The logging statement: | ||
** message: 'Logging statement.' | ||
* Secondary 2: The `throw` statement | ||
** message: 'Thrown exception.' | ||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
=== on 11 Oct 2014, 12:09:59 Freddy Mallet wrote: | ||
Sounds good to me! | ||
|
||
endif::env-github,rspecator-view[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
== 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[] |