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 for Anotar.Serilog #30

Closed j2jensen closed 6 years ago

j2jensen commented 6 years ago

The Anotar.Serilog.Fody package allows you to write code like this:

public class MyClass
{
    void MyMethod()
    {
        LogTo.Verbose("Message template with {SomeValue}", 42);
    }
}

And have it compile to something like this:

public class MyClass
{
    static ILogger logger = Log.ForContext<MyClass>();

    void MyMethod()
    {
        if (logger.IsEnabled(LogEventLevel.Verbose))
        {
            logger
                .ForContext("MethodName", "Void MyMethod()")
                .ForContext("LineNumber", 8)
                .Verbose("Message template with {SomeValue}", 42);
        }
    }
}

I'm switching from using Anotar.Log4Net to Serilog right now, and I love that this analyzer can warn me about common mistakes, but I would love to get the concise code and extra context that Anotar.Serilog provides. Since the Anotar.Serilog.LogTo static methods seem to mirror those of Serilog.Log, it seems like it might be an easy lift to make these analyzers provide the same analyses that they provide for Serilog.

Suchiman commented 6 years ago

If the logging methods would be annotated with Serilog.Core.MessageTemplateFormatMethodAttribute like in https://github.com/serilog/serilog/blob/2d4d8d13cffe33e5c72e6c6f4a29f84aaba8f5a3/src/Serilog/ILogger.cs#L93-L100 then it would be recognized automatically.

j2jensen commented 6 years ago

Very nice. I've created an Issue to ask if they can add those annotations.

https://github.com/Fody/Anotar/issues/123

j2jensen commented 6 years ago

The annotation is added, and everything works as expected. Thanks!