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

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

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

Most logging frameworks have methods that take a log level, an event ID or an exception as a separate input next to the log format and its arguments. There is a high chance that if the log level, the event ID or the exception are passed as the arguments to the message format, it was a mistake. This rule is going to raise in that scenario.

The rule covers the following logging frameworks:

How to fix it

Use the dedicated overload that takes the log level, event id, and/or exception as arguments.

Noncompliant code example

try { }
catch (Exception ex)
{
    logger.LogDebug("An exception occured {Exception} with {EventId}.", ex, eventId); // Noncompliant
}

Compliant solution

try { }
catch (Exception ex)
{
    logger.LogDebug(eventId, ex, "An exception occured.");
}

Exceptions

This rule will not raise an issue if one of the parameters mentioned above is passed twice, once as a separate argument to the invocation and once as an argument to the message format.

try { }
catch (Exception ex)
{
    logger.LogDebug(ex, "An exception occured {Exception}.", ex); // Compliant
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy