All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S6667.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

This rule raises an issue on logging calls inside a catch clause that does not pass the raised Exception.

Why is this an issue?

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:

How to fix it

Code examples

Noncompliant code example

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

public bool Save()
{
    try
    {
        DoSave();
        return true;
    }
    catch(IOException exception)
    {
        logger.LogError(exception, "Saving failed.");  // Compliant: Exception details are logged
        return false;
    }
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy