-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Update CA1873 to recommend source-generated logging with LoggerMessageAttribute #50584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…eAttribute Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
...ndamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/CA1873Example.vbproj
Outdated
Show resolved
Hide resolved
...entals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/CA1873Example.csproj
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR updates the CA1873 rule documentation to recommend modern source-generated logging with LoggerMessageAttribute instead of the older LoggerMessage pattern. The change aligns CA1873 with CA1848 and current .NET performance logging best practices introduced in .NET 6.
Key Changes
- Updated rule description and fix guidance to recommend source-generated logging with
LoggerMessageAttributeon partial methods - Replaced inline code examples with compilable C# and VB snippet files demonstrating violations (expensive operations like
string.Joinand object creation in logging arguments) and their fixes using source-generated logging - Added proper project files targeting .NET 10 with Microsoft.Extensions.Logging package references
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
docs/fundamentals/code-analysis/quality-rules/ca1873.md |
Updated rule description, fix guidance, and replaced inline code with snippet references; maintains proper AI-usage frontmatter and documentation structure |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Violation.cs |
C# example demonstrating violations: expensive string.Join operation and anonymous object creation in logging arguments |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Fix.cs |
C# example showing proper fix using source-generated logging with LoggerMessageAttribute on partial methods |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/CA1873Example.csproj |
Project file for C# examples targeting net10.0 with Microsoft.Extensions.Logging reference |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Program.cs |
Minimal program entry point for C# example project |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Violation.vb |
VB equivalent of C# violation examples showing same anti-patterns |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Fix.vb |
VB equivalent of C# fix examples using source-generated logging |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/CA1873Example.vbproj |
Project file for VB examples targeting net10.0 with Microsoft.Extensions.Logging reference |
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Program.vb |
Minimal program entry point for VB example project |
CA1873 documentation recommended the older
LoggerMessagepattern. Updated to recommend source-generated logging withLoggerMessageAttribute(available since .NET 6).Changes
LoggerMessagepattern toLoggerMessageAttributeon partial methodsstring.Join, object creation)Before
After
Aligns with CA1848 and current .NET performance logging best practices.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Internal previews