SerilogAnalyzer icon indicating copy to clipboard operation
SerilogAnalyzer copied to clipboard

Expected Serilog001 on extension method doesn't report warning when building with MSBuild

Open thorhj opened this issue 4 years ago • 0 comments

I have created extension methods over Serilog's ILogger. When I call these method incorrectly, embedding an exception in the message template instead of passing it as the first parameter, I expect the Serilog001 warning to appear.

using System;
using Serilog;
using Serilog.Core;

namespace BuggyAnalyzerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Expecting Serilog001 here:
            Log.Logger.MyError("The following exception was thrown: {Exception}", new Exception());
        }
    }

    static class LoggerExtensions
    {
        [MessageTemplateFormatMethod("messageTemplate")]
        public static void MyError(this ILogger logger, Exception ex, string messageTemplate, params object?[] propertyValues)
        {
#pragma warning disable Serilog004
            logger.Error(ex, messageTemplate, propertyValues);
#pragma warning restore
        }

        [MessageTemplateFormatMethod("messageTemplate")]
        public static void MyError(this ILogger logger, string messageTemplate, params object?[] propertyValues)
        {
#pragma warning disable Serilog004
            logger.Error(messageTemplate, propertyValues);
#pragma warning restore
        }
    }
}

When I build this code using dotnet build (using dotnet 3.1.100 SDK), the warning appears as expected. However, when I build using MSBuild (for instance through Visual Studio 16.8.4), the build is unexpectedly successful.

thorhj avatar Feb 03 '21 14:02 thorhj