dotnet / dotnet/sdk

CA1727 analyzer opt-in to allow OTel dotted.lower.case attribute names in log message template

Open
#50,807 2 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

### Is your feature request related to a problem? Please describe.

I am using some attributes from OpenTelemetry semantic conventions, like [`process.exit.code`](https://opentelemetry.io/docs/specs/semconv/registry/attributes/process/#process-exit-code), as placeholder names in [log message templates](https://learn.microsoft.com/dotnet/core/extensions/logging#log-message-template). The intention is to make these log entries searchable in the same way as activities. However, the CA1727 analyzer warns about such names. I have disabled the warnings by setting `dotnet_diagnostic.CA1727.severity = none` in `.editorconfig` but now it doesn't warn about `camelCase` names either.

### Describe the solution you'd like

Add to the CA1727 analyzer an opt-in mode in which it allows both `PascalCase` names (.NET convention) and `dotted.lower.case` names (OTel convention), where the OTel attribute name

* consists only of lower-case letters, digits, dots, and underscores
* and contains at least one dot

as per [General naming considerations](https://opentelemetry.io/docs/specs/semconv/general/naming/#general-naming-considerations).

It could be enabled with a new `dotnet_code_quality.CA1727.allow_dotted_lowercase` setting in `.editorconfig`.

Alternatively, `.editorconfig` could specify a regular expression and the analyzer would then allow names that match that. However, none of the [existing options for analyzers](https://github.com/dotnet/sdk/blob/v10.0.100-rc.1.25451.107/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Utilities/Compiler/Options/EditorConfigOptionNames.cs) seem to take a regex, and there might be a risk of denial-of-service attack if the regex is malicious.

Another alternative would be to implement a [DiagnosticSuppressor](https://learn.microsoft.com/dotnet/api/microsoft.codeanalysis.diagnostics.diagnosticsuppressor?view=roslyn-dotnet-4.13.0) for those names. However, the CA1727 diagnostic does not mention the placeholder name itself, and the Location of the diagnostic refers to the entire log message template, so the suppressor would have to parse the template again and reimplement most of the CA1727 analyzer.

### Additional context

Because dotted names like `process.exit.code` are not valid as identifiers in C#, using them with [compile-time logging source generation](https://learn.microsoft.com/dotnet/core/extensions/logger-message-generator) would require [TagNameAttribute](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.tagnameattribute?view=netstandard-2.0-pp).

Inspired by / .

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.