CustomRoslynAnalyzer 0.1.2
dotnet add package CustomRoslynAnalyzer --version 0.1.2
NuGet\Install-Package CustomRoslynAnalyzer -Version 0.1.2
<PackageReference Include="CustomRoslynAnalyzer" Version="0.1.2" />
<PackageVersion Include="CustomRoslynAnalyzer" Version="0.1.2" />
<PackageReference Include="CustomRoslynAnalyzer" />
paket add CustomRoslynAnalyzer --version 0.1.2
#r "nuget: CustomRoslynAnalyzer, 0.1.2"
#:package CustomRoslynAnalyzer@0.1.2
#addin nuget:?package=CustomRoslynAnalyzer&version=0.1.2
#tool nuget:?package=CustomRoslynAnalyzer&version=0.1.2
Roslyn Custom Analyzer package
CustomRoslynAnalyzer is a .NET Standard 2.0 analyzer library that ships as a NuGet package (the workflow already packs/publishes it) and exposes the following diagnostics to consumers:
| ID | Category | Description |
|---|---|---|
CR0001 |
Usage | Flags Console.WriteLine calls so you can enforce a logging abstraction instead of writing directly to standard output. |
CR0002 |
Naming | Requires public async methods to end with Async, making their asynchronous nature explicit. |
CR0003 |
Usage | Warns whenever traditional loops or lambda-based iterations invoke services deeper in Infrastructure namespaces, helping avoid repeated expensive work. |
CR0004 |
Usage | Prevents services or repositories (and methods that depend on them) from being used inside foreach loops or lambda iterations. |
Since these rules run inside the analyzer, any consuming project that references the NuGet package (or project reference with OutputItemType="Analyzer") will surface CR0001–CR0004 as compiler warnings.
Violations in SampleApp
SampleApp/Program.cs intentionally demonstrates the diagnostics emitted by the analyzer. In the loop below, the analyzer flags the Console.WriteLine call (CR0001) and the repetitive call to a service defined in SampleApp.Infrastructure (CR0003):
for (var i = 0; i < count; i++)
{
Trace.WriteLine($"Iteration {i + 1}/{count}");
await worker.FetchTelemetryAsync(); // violates CR0003 because `TelemetryApiClient` lives under SampleApp.Infrastructure
_ = await worker.Compute(); // violates CR0002 because Compute() do not end with `Async`
}
Console.WriteLine($"Telemetry fetched from {snapshot.Source}."); // violates CR0001
The analyzer also warns if any public async methods (such as BackgroundWorker.Compute) do not end with Async, which is how CR0002 stays enforced.
Next steps
- Add more
DiagnosticAnalyzerimplementations (and accompanying code fixes, if desired) underRules/. - Keep
AnalyzerReleases.Shipped.mdand.Unshipped.mdin sync with released rule IDs so versioned release notes stay accurate. - Add analyzer unit tests using
Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnitwhen you need automated verification of diagnostics.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.