Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 15, 2025

CA1873 documentation recommended the older LoggerMessage pattern. Updated to recommend source-generated logging with LoggerMessageAttribute (available since .NET 6).

Changes

  • Rule description and fix guidance: Changed from LoggerMessage pattern to LoggerMessageAttribute on partial methods
  • Code examples: Replaced inline code with compilable C# and VB snippets demonstrating:
    • Violations: expensive operations in logging arguments (string.Join, object creation)
    • Fixes: source-generated logging that defers evaluation until log level check

Before

// Fix: Guard with IsEnabled check
if (_logger.IsEnabled(LogLevel.Debug))
{
    _logger.LogDebug($"Processing {string.Join(", ", data)} items");
}

After

// Fix: Source-generated logging
public void ProcessData(int[] data)
{
    LogProcessingData(data);  // No expensive operation unless enabled
}

[LoggerMessage(Level = LogLevel.Debug, Message = "Processing {Data} items")]
private partial void LogProcessingData(int[] data);

Aligns with CA1848 and current .NET performance logging best practices.

Original prompt

This section details on the original issue you should resolve

<issue_title>CA1873 - Update logging recommendation to source generation</issue_title>
<issue_description>### Type of issue

Typo

Description

Change the recommendation and example for fixing this diagnostic from LoggerMessage to source-generated loggers.

Page URL

https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca1873

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1873.md

Document Version Independent Id

b592168a-cc0e-fe99-986e-c1a082228d92

Platform Id

8571c673-bf84-6950-9767-677e8a98e0d3

Article author

@gewarren

Metadata

  • ID: d46812eb-4194-0d2b-cce6-a3a658e5fd4d
  • PlatformId: 8571c673-bf84-6950-9767-677e8a98e0d3
  • Service: dotnet-fundamentals

Related Issues</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/quality-rules/ca1873.md CA1873: Avoid potentially expensive logging

Copilot AI changed the title [WIP] Update logging recommendation to source-generated loggers Update CA1873 to recommend source-generated logging with LoggerMessageAttribute Dec 15, 2025
Copilot AI requested a review from gewarren December 15, 2025 18:42
@gewarren gewarren marked this pull request as ready for review December 15, 2025 23:01
@gewarren gewarren requested a review from a team as a code owner December 15, 2025 23:01
Copilot AI review requested due to automatic review settings December 15, 2025 23:01
Copy link
Contributor

Copilot AI left a 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 LoggerMessageAttribute on partial methods
  • Replaced inline code examples with compilable C# and VB snippet files demonstrating violations (expensive operations like string.Join and 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

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @gewarren

Let's :shipit:

@gewarren gewarren merged commit c1b1868 into main Dec 16, 2025
16 of 17 checks passed
@gewarren gewarren deleted the copilot/update-logging-recommendation branch December 16, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA1873 - Update logging recommendation to source generation

3 participants