Skip to content

Commit bb4aee4

Browse files
authored
Reduce noise from CA1873 (#51818)
Several improvements to the analyzer: - Property accesses are common in logging calls, and property accesses are supposed to be cheap. Avoid raising diagnostics for property accesses. - GetType/GetHashCode/GetTimestamp are used reasonably-frequently in logging calls; special-case them to avoid diagnostics for them. - The main reason this rule exists is to eliminate cost on hot paths. Generally such hot paths aren't raising warning/error/critical diagnostics, such that the more rare warning/errors don't need as much attention to overheads. As such, I've changed the checks to only kick in by default for information and below, with a configuration switch that can be used to override to what levels it applies.
1 parent 6a4e43d commit bb4aee4

File tree

4 files changed

+859
-229
lines changed

4 files changed

+859
-229
lines changed

src/Microsoft.CodeAnalysis.NetAnalyzers/docs/Analyzer Configuration.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,31 @@ Default Value: `false`
877877

878878
Example: `dotnet_code_quality.CA1851.assume_method_enumerates_parameters = true`
879879

880+
### Maximum log level for expensive call analysis
881+
882+
Option Name: `max_log_level`
883+
884+
Configurable Rules: [CA1873](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/CA1873)
885+
886+
Option Values: One of the following log level values (case-insensitive):
887+
888+
| Option Value | Summary |
889+
| --- | --- |
890+
| `trace` | Analyze expensive calls at Trace level and below. |
891+
| `debug` | Analyze expensive calls at Debug level and below. |
892+
| `information` | Analyze expensive calls at Information level and below. |
893+
| `warning` | Analyze expensive calls at Warning level and below. |
894+
| `error` | Analyze expensive calls at Error level and below. |
895+
| `critical` | Analyze expensive calls at Critical level. |
896+
897+
This option configures the maximum log level for which CA1873 should flag potentially expensive operations in logging calls. Log levels higher than the configured value will not be analyzed, as they are typically always enabled in production scenarios.
898+
899+
Default Value: `information`
900+
901+
Example: `dotnet_code_quality.CA1873.max_log_level = warning`
902+
903+
With the above configuration, the analyzer will flag expensive operations in log calls at Trace, Debug, Information, and Warning levels, but not at Error or Critical levels.
904+
880905
### Proceed with analysis even if InternalsVisibleTo is present
881906

882907
Option Name: `ignore_internalsvisibleto`

0 commit comments

Comments
 (0)