-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d27458
Initial plan
Copilot 7178549
Update CA1873 to recommend source-generated logging with LoggerMessag…
Copilot 0a4348a
Fix code examples to properly avoid expensive operations in logging
Copilot 619965d
Apply suggestions from code review
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...als/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/CA1873Example.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
29 changes: 29 additions & 0 deletions
29
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Fix.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| // <FixExample> | ||
| partial class FixExample | ||
| { | ||
| private readonly ILogger _logger; | ||
|
|
||
| public FixExample(ILogger<FixExample> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public void ProcessData(int[] data) | ||
| { | ||
| // Fixed: use source-generated logging. | ||
| // The data array is passed directly; no expensive operation executed unless log level is enabled. | ||
| LogProcessingData(data); | ||
|
|
||
| // Fixed: use source-generated logging. | ||
| LogTraceData(data.Length, data); | ||
| } | ||
|
|
||
| [LoggerMessage(Level = LogLevel.Debug, Message = "Processing {Data} items")] | ||
| private partial void LogProcessingData(int[] data); | ||
|
|
||
| [LoggerMessage(Level = LogLevel.Trace, Message = "Data: Count={Count}, Items={Items}")] | ||
| private partial void LogTraceData(int count, int[] items); | ||
| } | ||
| // </FixExample> |
2 changes: 2 additions & 0 deletions
2
.../fundamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
| Console.WriteLine("Hello, World!"); |
22 changes: 22 additions & 0 deletions
22
...undamentals/code-analysis/quality-rules/snippets/ca1873/csharp/CA1873Example/Violation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| // <ViolationExample> | ||
| class ViolationExample | ||
| { | ||
| private readonly ILogger _logger; | ||
|
|
||
| public ViolationExample(ILogger<ViolationExample> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public void ProcessData(int[] data) | ||
| { | ||
| // Violation: expensive operation in logging argument. | ||
| _logger.LogDebug($"Processing {string.Join(", ", data)} items"); | ||
|
|
||
| // Violation: object creation in logging argument. | ||
| _logger.LogTrace("Data: {Data}", new { Count = data.Length, Items = data }); | ||
| } | ||
| } | ||
| // </ViolationExample> |
13 changes: 13 additions & 0 deletions
13
...mentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/CA1873Example.vbproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <RootNamespace>CA1873Example</RootNamespace> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
28 changes: 28 additions & 0 deletions
28
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Fix.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| Imports Microsoft.Extensions.Logging | ||
|
|
||
| ' <FixExample> | ||
| Partial Class FixExample | ||
| Private ReadOnly _logger As ILogger | ||
|
|
||
| Public Sub New(logger As ILogger(Of FixExample)) | ||
| _logger = logger | ||
| End Sub | ||
|
|
||
| Public Sub ProcessData(data As Integer()) | ||
| ' Fixed: use source-generated logging. | ||
| ' The data array is passed directly; no expensive operation executed unless log level is enabled. | ||
| LogProcessingData(data) | ||
|
|
||
| ' Fixed: use source-generated logging. | ||
| LogTraceData(data.Length, data) | ||
| End Sub | ||
|
|
||
| <LoggerMessage(Level:=LogLevel.Debug, Message:="Processing {Data} items")> | ||
| Private Partial Sub LogProcessingData(data As Integer()) | ||
| End Sub | ||
|
|
||
| <LoggerMessage(Level:=LogLevel.Trace, Message:="Data: Count={Count}, Items={Items}")> | ||
| Private Partial Sub LogTraceData(count As Integer, items As Integer()) | ||
| End Sub | ||
| End Class | ||
| ' </FixExample> |
7 changes: 7 additions & 0 deletions
7
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Program.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Imports System | ||
|
|
||
| Module Program | ||
| Sub Main(args As String()) | ||
| Console.WriteLine("Hello World!") | ||
| End Sub | ||
| End Module |
19 changes: 19 additions & 0 deletions
19
docs/fundamentals/code-analysis/quality-rules/snippets/ca1873/vb/CA1873Example/Violation.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Imports Microsoft.Extensions.Logging | ||
|
|
||
| ' <ViolationExample> | ||
| Class ViolationExample | ||
| Private ReadOnly _logger As ILogger | ||
|
|
||
| Public Sub New(logger As ILogger(Of ViolationExample)) | ||
| _logger = logger | ||
| End Sub | ||
|
|
||
| Public Sub ProcessData(data As Integer()) | ||
| ' Violation: expensive operation in logging argument. | ||
| _logger.LogDebug($"Processing {String.Join(", ", data)} items") | ||
|
|
||
| ' Violation: object creation in logging argument. | ||
| _logger.LogTrace("Data: {Data}", New With {.Count = data.Length, .Items = data}) | ||
| End Sub | ||
| End Class | ||
| ' </ViolationExample> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.