CSharp-LocalSonarAnalizer 1.0.3

dotnet add package CSharp-LocalSonarAnalizer --version 1.0.3
                    
NuGet\Install-Package CSharp-LocalSonarAnalizer -Version 1.0.3
                    
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="CSharp-LocalSonarAnalizer" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharp-LocalSonarAnalizer" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="CSharp-LocalSonarAnalizer" />
                    
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 CSharp-LocalSonarAnalizer --version 1.0.3
                    
#r "nuget: CSharp-LocalSonarAnalizer, 1.0.3"
                    
#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 CSharp-LocalSonarAnalizer@1.0.3
                    
#: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=CSharp-LocalSonarAnalizer&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=CSharp-LocalSonarAnalizer&version=1.0.3
                    
Install as a Cake Tool

CSharp-LocalSonarAnalyzer

Advanced static code analyzer for C# that uses SonarAnalyzer.CSharp and SonarQube rules. Designed for local use in debug mode only.

Features

  • Static code analysis using SonarQube rules
  • Local debugging capabilities
  • Code coverage reporting
  • Build artifact cleanup for production deployments

Prerequisites

  • .NET SDK (version X.X or higher)
  • SonarAnalyzer.CSharp package
  • Coverlet for code coverage (already configured)

Configuration

Code Coverage Setup

  1. Create a coverlet.runsettings file in your Application.Test project
  2. Add the following configuration:
<?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>
  1. In your application, you must install the Nuget package https://www.nuget.org/packages/Microsoft.CodeAnalysis.Workspaces.MSBuild/

Production Deployment Cleanup (Optional)

To prevent generation of unnecessary BuildHost folders during deployment, add this to your startup project's .csproj file:

<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>

Usage

  1. Configure the coverlet.runsettings as shown above
  2. Build the solution in Debug mode
  3. Run the analyzer against your target project
  4. Review analysis results in your IDE or output window

Troubleshooting

  • BuildHost folders still appearing: Ensure the Target is added to the correct project file and the Condition matches your release configuration
  • Coverage not reported: Verify the runsettings file is in the correct location and includes match your project structure

Notes

  • This analyzer is intended for local development use only
  • For production analysis, consider using a full SonarQube server installation
  • Remember to exclude test assemblies from coverage reports for accurate metrics
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.3 238 7/25/2025
1.0.2 367 7/24/2025
1.0.1 368 7/24/2025
1.0.0 108 7/17/2025