CodeAnalyzerGenerator 1.0.0

dotnet add package CodeAnalyzerGenerator --version 1.0.0
                    
NuGet\Install-Package CodeAnalyzerGenerator -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeAnalyzerGenerator" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeAnalyzerGenerator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CodeAnalyzerGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeAnalyzerGenerator --version 1.0.0
                    
#r "nuget: CodeAnalyzerGenerator, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeAnalyzerGenerator@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeAnalyzerGenerator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CodeAnalyzerGenerator&version=1.0.0
                    
Install as a Cake Tool

CodeAnalyzerGenerator

Description

An advanced static analyzer for C# code that uses the Roslyn framework (Microsoft.CodeAnalysis) to perform static code analysis. Designed specifically for local use in debug mode, allowing developers to perform code analysis and test coverage using .NET BuildHost.

Key Features

  • โœจ Static code analysis using Microsoft.CodeAnalysis (Roslyn)
  • ๐Ÿ“Š Code coverage report generation
  • ๐ŸŽจ Interactive console interface with color codes
  • ๐Ÿ“ Reports in multiple formats (Console and HTML)
  • ๐Ÿ” Specific analysis coverage according to configuration
  • ๐Ÿš€ Parallel execution for better performance
  • ๐Ÿงน Cleanup of compilation artifacts

Prerequisites (optional)

  • Coverlet (configured for code coverage)

Installation and Configuration

1. Project Configuration

// En Program.cs o Startup.cs
services.AddLocalSonarAnalizer(options =>
{
    options.UseDebugConsole = true; // Enable the interactive console
});

2. Available Log Levels

The interactive console provides different types of messages with color codes:

  • ๐ŸŸฆ Info (White): General information
  • ๐ŸŸฉ Success (Green): Successful operations
  • ๐ŸŸจ Warning (Yellow): Warnings
  • ๐ŸŸฅ Error (Dark Red): Errors
  • ๐Ÿ”ด Critical (Red): Critical errors
  • ๐ŸŸฆ Highlight (Cyan): Highlighted information

3. Code Coverage Configuration

The coverlet.runsettings file is required for coverage configuration. You have two options:

  1. Automatic: If the file does not exist, it will be generated automatically at runtime.
  2. Manual: Create the file in your test project with this structure:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>cobertura</Format>
          
          <Include>
              [Application]*CommandHandler*
              [Application]*QueryHandler*
          </Include>
          
          <Exclude>
              [xunit.*]*,
              [Moq]*,
              [FluentResults]*,
              [SharedKernel]*,
              [Domain]*,
              [Web.Api]*,
              [Infrastructure]*
          </Exclude>
          
          <ExcludeByAttribute>ExcludeFromCodeCoverage</ExcludeByAttribute>
          
          <ExcludeByFile>
              **/bin/**/*,
              **/obj/**/*,
              **/TestResults/**/*,
              **/Models/**/*,
              **/DependencyInjection.cs
          </ExcludeByFile>
           
          <IncludeTestAssembly>false</IncludeTestAssembly>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

4. Cleanup Configuration for Production (Optional)

To prevent unnecessary BuildHost folders from being generated during deployment:

<Target Name="DeleteBuildHostFoldersFromOutput" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'Release'">
    <Message Text="[RELEASE POST-BUILD] Deleting BuildHost folders from Web.Api output: $(TargetDir)" Importance="high" />
    <RemoveDir Directories="$(TargetDir)BuildHost-net472" />
    <RemoveDir Directories="$(TargetDir)BuildHost-netcore" />
</Target>

Use

Public API

// Basic analysis
await analyzer.Analyze(solutionPath);

// Include test projects in the analysis
await analyzer.Analyze(solutionPath, analyzeRulesTestProjects: true);

// Complete analysis with coverage
await analyzer.Analyze(solutionPath, analyzeRulesTestProjects: true, runTestsAndCalculateCoverage: true);

// Complete analysis with HTML report
await analyzer.Analyze(solutionPath, analyzeRulesTestProjects: true, runTestsAndCalculateCoverage: true, outputHtml: true);

Generated Reports

The tool generates two types of reports:

  1. Analysis Report:

    • Problems detected by the static analyzer
    • Severity of problems (using Roslyn's DiagnosticSeverity)
    • Exact location in the code
    • Analysis time
  2. Coverage Report:

    • Coverage percentage per project
    • Details of classes with incomplete coverage
    • Uncovered lines
    • Partially covered conditions

Troubleshooting

BuildHost Folders

Problem: BuildHost folders continue to appear Solution:

  • Verify that the Target is in the correct .csproj file
  • Confirm that the condition matches the release configuration

Coverage Not Reported

Problem: Coverage report is not generated Solution:

  • Verify the existence of the runsettings file
  • Check that the inclusion patterns match the project structure
  • Verify that coverlet.collector is referenced in the test project

Important Notes

  • ๐Ÿ”’ Designed for local use in development
  • ๐Ÿ› ๏ธ Uses .NET BuildHost and Roslyn for static analysis
  • ๐Ÿ“Š Reports exclude test assemblies for accurate metrics
  • โšก Analysis runs in parallel using all available cores
  • ๐ŸŽจ Interactive console improves the development experience

License

This project is licensed under the terms of the MIT license.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 93 8/13/2025