dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.52k stars 4.53k forks source link

Strange behavior while using compile-time logging source generation without message template #101687

Open reyang opened 2 months ago

reyang commented 2 months ago

Description

When using compile-time logging source generation from Micrososft.Extensions.Logging.Abstraction version 8.0 and the message template provided:

internal static partial class LoggerExtensions
{
    [LoggerMessage(LogLevel.Critical)]
    public static partial void FoodRecallNotice(
        this ILogger logger,
        string brandName,
        string productDescription,
        string productType,
        string recallReasonDescription,
        string companyName);
}

logger.FoodRecallNotice(
    brandName: "Contoso",
    productDescription: "Salads",
    productType: "Food & Beverages",
    recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
    companyName: "Contoso Fresh Vegetables, Inc.");
  1. The warning message seems to be confusing:

    D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\Program.cs(39,16): warning SYSLIB1015: Argument 'brandNa
    me' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/sysl
    ib1015) [D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\getting-started-console.csproj::TargetFramework
    =net8.0]
    D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\Program.cs(40,16): warning SYSLIB1015: Argument 'product
    Description' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnos
    tics/syslib1015) [D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\getting-started-console.csproj::Target
    Framework=net8.0]
    D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\Program.cs(41,16): warning SYSLIB1015: Argument 'product
    Type' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/sy
    slib1015) [D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\getting-started-console.csproj::TargetFramewo
    rk=net8.0]
    D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\Program.cs(42,16): warning SYSLIB1015: Argument 'recallR
    easonDescription' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-di
    agnostics/syslib1015) [D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\getting-started-console.csproj::T
    argetFramework=net8.0]
    D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\Program.cs(43,16): warning SYSLIB1015: Argument 'company
    Name' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/sy
    slib1015) [D:\repo\opentelemetry-dotnet\docs\logs\getting-started-console\getting-started-console.csproj::TargetFramewo
    rk=net8.0]
  2. The console output doesn't seem to be helpful:

    crit: Program[1338249384]

Reproduction Steps

Check https://github.com/open-telemetry/opentelemetry-dotnet/pull/5553#issuecomment-2067316752

Expected behavior

Instead of giving 5 warnings, it'll be better to provide 1 INFO level message (e.g. "The message template is not specified; a default template will be generated"). In addition, the console logger provider should output all the key-value pairs.

Alternatively, aligning with Microsoft.Extensions.Logging.Abstraction, as described in https://github.com/open-telemetry/opentelemetry-dotnet/pull/5553#issuecomment-2067320523.

Actual behavior

User gets 5 warnings, which normally break the build due to "treat warnings as errors" setting. The console output only shows crit: Program[1338249384] without any key-value pairs.

Regression?

No

Known Workarounds

Provide a message template with all the key-value pairs.

Configuration

No response

Other information

No response

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/area-extensions-logging See info in area-owners.md if you want to be subscribed.