dotnet / dotnet/sdk

CA1848 on `LoggerExtensions.BeginScope()` contradicts documentation on high-performance logging

Open
#54,236 1 comment 0 reactions 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
Creating a logging scope with format args triggers CA1848 with the suggestion `For improved performance, use the LoggerMessage delegates instead of calling 'LoggerExtensions.BeginScope(ILogger, string, params object?[])'`, even though the use of 'LoggerExtensions.BeginScope(ILogger, string, params object?[])' is explicitly prescribed by and used in example code in official documentation on high-performance logging (https://learn.microsoft.com/en-us/dotnet/core/extensions/logging/high-performance-logging#define-logger-message-scope-with-source-generation).

Note that the compiler's suggestion is identical to the one given for `ILogger.Log` calls, which documentation recommends replacing with `LoggerMessageAttribute`-decorated methods for source-generated logging while explicitly noting `LoggerMessage.Define()` as legacy.
The documentation page for CA1848 itself (https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca1848) also gives the following advice on how to fix this violation: "Use LoggerMessageAttribute to fix violations of this rule. (Or, if you're using .NET 5 or earlier, use the LoggerMessage class.)"
For `BeginScope`, this is impossible to implement because LoggerMessageAttribute requires target methods to return `void` rather than `IDisposable?`.

If the compiler's suggestion is meant to refer to the use of a delegate created via `LoggerMessage.DefineScope()` (a method never even mentioned in the documentation), documentation should recommend this approach instead. Otherwise the compiler should not trigger CA1848 for explicitly prescribed use patterns.

### To Reproduce

```.editorconfig
[*.cs]
dotnet_diagnostic.CA1848.severity = warning
# or dotnet_analyzer_diagnostic.category-Performance.severity = warning
```

```c#
public class Foo(ILogger logger)
{
public void Bar()
{
using (logger.BeginScope("Processing scope, started at: {DateTime}", DateTime.Now)) // CA1848
{
}
}
}
```

### Further technical details
details of dotnet --info


.NET SDK:
Version: 10.0.102
Commit: 4452502459
Workload version: 10.0.100-manifests.73000c7b
MSBuild version: 18.0.7+445250245

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.102\

.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.2
Architecture: x64
Commit: 4452502459

.NET SDKs installed:
9.0.202 [C:\Program Files\dotnet\sdk]
9.0.301 [C:\Program Files\dotnet\sdk]
10.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

- The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version: VS Code 1.115.0

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.