Skip to content

Fix CA1848 documentation: Update link and add code examples #47579

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 25, 2025

This PR improves the CA1848 rule documentation by updating an incorrect link and adding practical code examples to help developers understand how to fix violations.

Changes Made

Updated LoggerMessage Link

The rule description previously linked to the LoggerMessage API documentation, but now correctly points to the high-performance logging article that provides comprehensive guidance on implementing the pattern:

  • Before: Links to Microsoft.Extensions.Logging.LoggerMessage API docs
  • After: Links to /dotnet/core/extensions/high-performance-logging article

Added Code Examples

Added clear before/after examples in the "How to fix violations" section showing:

Code that violates CA1848:

public class SomethingDoer
{
   private readonly ILogger _logger;
   public SomethingDoer(ILogger<SomethingDoer> logger)
   {
    _logger = logger;
   }

   public void DoSomething()
   {
       _logger.LogInformation("We did something!");  //This call violates CA1848
   }
}

Corrected code using LoggerMessage pattern:

public partial class SomethingDoer
{
   private readonly ILogger _logger;
   public SomethingDoer(ILogger<SomethingDoer> logger)
   {
    _logger = logger;
   }

   public void DoSomething()
   {
       Log_DidSomething();
   }

   [LoggerMessage(Level = LogLevel.Information, Message = "We did something!")]
   private partial void Log_DidSomething();
}

These examples demonstrate the key differences:

  • Using partial class declaration
  • Replacing direct ILogger extension method calls with generated delegate methods
  • Using the [LoggerMessage] attribute to define the log message template

The documentation now provides developers with concrete examples they can reference when fixing CA1848 violations in their code.

Fixes #46239.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Update LoggerMessage link to point to high-performance logging article
- Add before/after code examples showing CA1848 violation and fix
- Examples demonstrate proper LoggerMessage pattern usage

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add example fix to CA1848 Fix CA1848 documentation: Update link and add code examples Jul 25, 2025
@Copilot Copilot AI requested a review from gewarren July 25, 2025 18:24
Copilot finished work on behalf of gewarren July 25, 2025 18:24
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.

Add example fix to CA1848
2 participants