Skip to content

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

Open
@reyang

Description

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]
  1. The console output doesn't seem to be helpful:
crit: Program[1338249384]

Reproduction Steps

Check open-telemetry/opentelemetry-dotnet#5553 (comment)

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 open-telemetry/opentelemetry-dotnet#5553 (comment).

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions