Skip to content

Commit 1b4a249

Browse files
authored
New rule MA0124: Log Parameter type is not valid (#433)
1 parent 3d7b2e1 commit 1b4a249

File tree

12 files changed

+775
-5
lines changed

12 files changed

+775
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,8 @@ If you are already using other analyzers, you can check [which rules are duplica
139139
|[MA0121](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0121.md)|Design|Do not overwrite parameter value|ℹ️|||
140140
|[MA0122](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0122.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes are only valid in routable components (@page)|ℹ️|✔️||
141141
|[MA0123](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0123.md)|Design|Sequence number must be a constant|⚠️|✔️||
142+
|[MA0124](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0124.md)|Design|Log Parameter type is not valid|⚠️|✔️||
143+
|[MA0125](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0125.md)|Design|The list of log parameter types contains an invalid type|⚠️|✔️||
144+
|[MA0126](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0126.md)|Design|The list of log parameter types contains a duplicate|⚠️|✔️||
142145

143146
<!-- rules -->

docs/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
|[MA0121](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0121.md)|Design|Do not overwrite parameter value|<span title='Info'>ℹ️</span>|||
124124
|[MA0122](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0122.md)|Design|Parameters with \[SupplyParameterFromQuery\] attributes are only valid in routable components (@page)|<span title='Info'>ℹ️</span>|✔️||
125125
|[MA0123](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0123.md)|Design|Sequence number must be a constant|<span title='Warning'>⚠️</span>|✔️||
126+
|[MA0124](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0124.md)|Design|Log Parameter type is not valid|<span title='Warning'>⚠️</span>|✔️||
127+
|[MA0125](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0125.md)|Design|The list of log parameter types contains an invalid type|<span title='Warning'>⚠️</span>|✔️||
128+
|[MA0126](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0126.md)|Design|The list of log parameter types contains a duplicate|<span title='Warning'>⚠️</span>|✔️||
126129

127130
# .editorconfig - default values
128131

@@ -492,6 +495,15 @@ dotnet_diagnostic.MA0122.severity = suggestion
492495
493496
# MA0123: Sequence number must be a constant
494497
dotnet_diagnostic.MA0123.severity = warning
498+
499+
# MA0124: Log Parameter type is not valid
500+
dotnet_diagnostic.MA0124.severity = warning
501+
502+
# MA0125: The list of log parameter types contains an invalid type
503+
dotnet_diagnostic.MA0125.severity = warning
504+
505+
# MA0126: The list of log parameter types contains a duplicate
506+
dotnet_diagnostic.MA0126.severity = warning
495507
```
496508

497509
# .editorconfig - all rules disabled
@@ -862,4 +874,13 @@ dotnet_diagnostic.MA0122.severity = none
862874
863875
# MA0123: Sequence number must be a constant
864876
dotnet_diagnostic.MA0123.severity = none
877+
878+
# MA0124: Log Parameter type is not valid
879+
dotnet_diagnostic.MA0124.severity = none
880+
881+
# MA0125: The list of log parameter types contains an invalid type
882+
dotnet_diagnostic.MA0125.severity = none
883+
884+
# MA0126: The list of log parameter types contains a duplicate
885+
dotnet_diagnostic.MA0126.severity = none
865886
```

docs/Rules/MA0124.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# MA0124 - Log Parameter type is not valid
2+
3+
This rule ensures the parameters for Microsoft.Extensions.Logging's logger are of the expected types. You can configure the expected types using a configuration file.
4+
5+
To configure the rule, you need to create a file named `LoggerParameterTypes.txt` or `LoggerParameterTypes.*.txt`.
6+
- Each line is of the form `PropertyName;ExpectedType1;ExpectedType2;ExpectedType3;...`
7+
- A type is represented by its CLR metadata name or [XML Comment ID](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/documentation-comments.md#d42-id-string-format)
8+
- Lines starting with `#` are comments
9+
10+
````
11+
# This is a comment
12+
13+
Name;System.String
14+
Count;System.Int32;System.Int64
15+
Length;T:System.Nullable{System.Int32}
16+
````
17+
18+
Then, you need to add the file to the `AdditionalFiles` collection in the `csproj` file:
19+
20+
````xml
21+
<Project Sdk="Microsoft.NET.Sdk">
22+
23+
<ItemGroup>
24+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\LoggerParameterTypes.txt" />
25+
</ItemGroup>
26+
27+
</Project>
28+
````
29+
30+
Then, the analyzer reports log parameters of the wrong type:
31+
32+
````
33+
using Microsoft.Extensions.Logging;
34+
35+
ILogger logger = ...;
36+
logger.LogInformation("{Name}", 123); # report diagnostic because the configuration file indicates Name should be of type string
37+
logger.LogInformation("{Name}", "dummy");
38+
````
39+

docs/Rules/MA0125.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# MA0125 - The list of log parameter types contains an invalid type
2+
3+
The configuration file for the rule [MA0124](./MA0124.md) contains a type that is not found in the current compilation.
4+
5+
````
6+
PropName;System.Unknown type # Report diagnostic
7+
````

docs/Rules/MA0126.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# MA0126 - The list of log parameter types contains a duplicate
2+
3+
The configuration file for the rule [MA0124](./MA0124.md) contains duplicated names.
4+
5+
````
6+
PropName;type
7+
PropName;type # Report diagnostic
8+
````

src/Meziantou.Analyzer/Configurations/AnalyzerOptionsExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static string GetConfigurationValue(this AnalyzerOptions options, SyntaxT
1717
return defaultValue;
1818
}
1919

20+
public static string GetConfigurationValue(this AnalyzerOptions options, IOperation operation, string key, string defaultValue)
21+
{
22+
return GetConfigurationValue(options, operation.Syntax.SyntaxTree, key, defaultValue);
23+
}
24+
2025
public static bool GetConfigurationValue(this AnalyzerOptions options, SyntaxTree syntaxTree, string key, bool defaultValue)
2126
{
2227
var configuration = options.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);

src/Meziantou.Analyzer/Internals/CompilationExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System.Collections.Generic;
2+
using System.Collections.Immutable;
23
using System.Diagnostics;
34
using Microsoft.CodeAnalysis;
45

56
namespace Meziantou.Analyzer;
67

78
internal static class CompilationExtensions
89
{
9-
public static IEnumerable<INamedTypeSymbol> GetTypesByMetadataName(this Compilation compilation, string typeMetadataName)
10+
public static ImmutableArray<INamedTypeSymbol> GetTypesByMetadataName(this Compilation compilation, string typeMetadataName)
1011
{
12+
var result = ImmutableArray.CreateBuilder<INamedTypeSymbol>();
1113
var symbol = compilation.Assembly.GetTypeByMetadataName(typeMetadataName);
1214
if (symbol != null)
13-
yield return symbol;
15+
{
16+
result.Add(symbol);
17+
}
1418

1519
foreach (var reference in compilation.References)
1620
{
@@ -20,8 +24,12 @@ public static IEnumerable<INamedTypeSymbol> GetTypesByMetadataName(this Compilat
2024

2125
symbol = assemblySymbol.GetTypeByMetadataName(typeMetadataName);
2226
if (symbol != null)
23-
yield return symbol;
27+
{
28+
result.Add(symbol);
29+
}
2430
}
31+
32+
return result.ToImmutable();
2533
}
2634

2735
// Copy from https://github.com/dotnet/roslyn/blob/d2ff1d83e8fde6165531ad83f0e5b1ae95908289/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/CompilationExtensions.cs#L11-L68

src/Meziantou.Analyzer/RuleIdentifiers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ internal static class RuleIdentifiers
126126
public const string DoNotOverwriteRazorComponentParameterValue = "MA0121";
127127
public const string SupplyParameterFromQueryRequiresRoutableComponent = "MA0122";
128128
public const string SequenceNumberMustBeAConstant = "MA0123";
129+
public const string LoggerParameterType = "MA0124";
130+
public const string LoggerParameterType_InvalidType = "MA0125";
131+
public const string LoggerParameterType_DuplicateRule = "MA0126";
129132

130133
public static string GetHelpUri(string idenfifier)
131134
{

0 commit comments

Comments
 (0)