![github-actions[bot]](/assets/img/avatar_default.png)
* Create rule S6667 * Add csharp description * Add link to Serilog Serilog001: Exception Usage * Review comments * Add VB descriptions and samples * Add VB * Code review * Add logging tag --------- Co-authored-by: martin-strecker-sonarsource <martin-strecker-sonarsource@users.noreply.github.com> Co-authored-by: Martin Strecker <martin.strecker@sonarsource.com> Co-authored-by: Costin Zaharia <costin.zaharia@sonarsource.com>
47 lines
886 B
Plaintext
47 lines
886 B
Plaintext
This rule raises an issue on logging calls inside a `catch` clause that does not pass the raised `Exception`.
|
|
|
|
include::../why-dotnet.adoc[]
|
|
|
|
== How to fix it
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public bool Save()
|
|
{
|
|
try
|
|
{
|
|
DoSave();
|
|
return true;
|
|
}
|
|
catch(IOException)
|
|
{
|
|
logger.LogError("Saving failed."); // Noncompliant: No specifics about the error are logged
|
|
return false;
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
public bool Save()
|
|
{
|
|
try
|
|
{
|
|
DoSave();
|
|
return true;
|
|
}
|
|
catch(IOException exception)
|
|
{
|
|
logger.LogError(exception, "Saving failed."); // Compliant: Exception details are logged
|
|
return false;
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../resources-dotnet.adoc[] |