Suchiman / SerilogAnalyzer

Roslyn-based analysis for code using the Serilog logging library. Checks for common mistakes and usage problems.
Apache License 2.0
309 stars 29 forks source link

Support use of MessageTemplateFormatMethod on extension methods #39

Closed GitSnafu closed 5 years ago

GitSnafu commented 5 years ago

This is similar to issue #19 but concerns extension methods and the Exception argument rule and is perhaps best illustrated by an example.

public interface IMyLogger {}

public static class MyLoggerExtensions
{
    [MessageTemplateFormatMethod("messageTemplate")]
    public static void Warning(this IMyLogger logger, string messageTemplate, object arg) {}
}

Given the above, the analyzer currently fails to report on the misplaced exception in cases such as these:

IMyLogger log = null;
log.Warning("Hello World", ex);
// And
MyLoggerExtensions.Warning(log, "Hello World", ex);

Given the current implementation this makes sense as the assumption is that the Exception parameter is at position one, which obviously isn't true in the case of an extension method. I've got a repro and proposed fix that I can push for you to look at and merge if you're happy with it.

Note that the missing property in the template string is still detected and reported.

Thanks for a great analyzer, it's really indispensable once you start using Serilog extensively.

Suchiman commented 5 years ago

Sure, i'm happy to take a look at your fix 👍

GitSnafu commented 5 years ago

Great, I've submitted a PR you can take a look at. Cheers!

Suchiman commented 5 years ago

I've just pushed version 0.15 with your fix inside. Thanks again for the PR 👍

GitSnafu commented 5 years ago

I've just pushed version 0.15 with your fix inside. Thanks again for the PR 👍

Thanks a lot. :)