This rule raises an issue on logging calls inside a catch clause that does not pass the raised Exception.
A log entry should contain all the relevant information about the current execution context. The Exception raised in a catch block not only provides the message but also:
Logging methods provide overloads that
accept an Exception as a parameter and logging
providers persist the Exception in a structured way to facilitate the tracking of system failures. Therefore Exceptions
should be passed to the logger.
The rule covers the following logging frameworks:
public bool Save()
{
try
{
DoSave();
return true;
}
catch(IOException)
{
logger.LogError("Saving failed."); // Noncompliant: No specifics about the error are logged
return false;
}
}
public bool Save()
{
try
{
DoSave();
return true;
}
catch(IOException exception)
{
logger.LogError(exception, "Saving failed."); // Compliant: Exception details are logged
return false;
}
}
try-catch statement Serilog001: Exception
Usage