agoda-com / agoda-com/AgodaAnalyzers

Passing exception into Logger as message arg

Ouverte
#168 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
enhancement New Rule Proposal
Langage dominant
C#
Étoiles
25
Forks
15
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

# Feature request

## Type

- [x] - Enhancement - completely new feature
- [ ] - Improvement - make what we have better

## Is your feature request related to a problem?

`Microsoft.Extensions.Logging.LoggerExtensions` have several overloads that take exceptions. The signature is usually in the format of
```csharp
public static void LogInformation(this ILogger logger, Exception exception, string message, params object[] args)
```
the exception being the first parameter of the extension method.

There is another overload, that doesn't take an exception:
```csharp
public static void LogInformation(this ILogger logger, string message, params object[] args)
````
People often make a mistake of using this overload, assuming that the exception is the second parameter, like
```csharp
_logger.LogError("Something went wrong", ex);
```
This will result in logs that doesn't contain any information about the actual exception, since it will just try to fit `ex` into the message template.

## Describe the solution you'd like

Show warning if an exception is passed as a parameter to an overload that does not take an exception as a parameter. Even if the intent is to pass the exception into the message template, I believe we should use the overload that also takes the message.

## Additional context

N/A

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.