Skip to content

CA1873 False positive for guarded non-extension logger calls in .NET 10 #50893

@ArturDorochowicz

Description

@ArturDorochowicz

Describe the bug

CA1873 analyzer doesn't detect guarded non-extension logger calls and reports false positive.

To Reproduce

Take this code:

using Microsoft.Extensions.Logging;

sealed partial class Class1(ILogger<Class1> logger)
{
    public void M()
    {
        if (logger.IsEnabled(LogLevel.Information))
        {
            Log(LogLevel.Information, GetType());
        }
        
        if (logger.IsEnabled(LogLevel.Information))
        {
            Log(logger, LogLevel.Information, GetType());
        }
    }
    
    [LoggerMessage("{Type}")]
    partial void Log(LogLevel logLevel, Type type);
    
    [LoggerMessage("{Type}")]
    static partial void Log(ILogger logger, LogLevel logLevel, Type type);
}

and compile:

$ dotnet build -p:AnalysisMode=Recommended
Restore complete (0.7s)
    info NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  sdk-10-0-100-rc-1 succeeded with 2 warning(s) (0.4s) → bin/Debug/net10.0/sdk-10-0-100-rc-1.dll
    [...]/Class1.cs(9,39): warning CA1873: Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
    [...]/Class1.cs(14,47): warning CA1873: Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)

Build succeeded with 2 warning(s) in 1.7s

This reproduces with 10.0.100-rc1 SDK.

Exceptions (if any)

Further technical details

details of dotnet --info

.NET SDK: Version: 10.0.100-rc.1.25451.107 Commit: 2db1f5ee2b Workload version: 10.0.100-manifests.1a2d104c MSBuild version: 17.15.0-preview-25451-107+2db1f5ee2

Runtime Environment:
OS Name: ubuntu
OS Version: 24.04
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/10.0.100-rc.1.25451.107/

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.0-rc.1.25451.107
Architecture: x64
Commit: 2db1f5ee2b

.NET SDKs installed:
10.0.100-rc.1.25451.107 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 10.0.0-rc.1.25451.107 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 10.0.0-rc.1.25451.107 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
DOTNET_GENERATE_ASPNET_CERTIFICATE [false]
DOTNET_NOLOGO [true]
DOTNET_ROLL_FORWARD [Major]
DOTNET_RUNNING_IN_CONTAINER [true]
DOTNET_SDK_VERSION [10.0.100-rc.1.25451.107]
DOTNET_USE_POLLING_FILE_WATCHER [true]
DOTNET_VERSION [10.0.0-rc.1.25451.107]

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions