rspec/rules/S6667/csharp/rule.adoc
github-actions[bot] a6f17497f7
Create rule S6667: Exceptions should be passed as an argument when logging in a catch clause (#2516)
* 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>
2024-02-22 15:46:16 +01:00

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