dotnet / dotnet/sdk

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

Open
#50,893 3 comments 1 reaction 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

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

### To Reproduce

Take this code:
```csharp
using Microsoft.Extensions.Logging;

sealed partial class Class1(ILogger 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.