~/blog/7

$ cat post-7.log

Published

#C# #.NET #Code Quality #SonarQube

I've heard about tools like this but never really used them myself. In my last team, SonarQube ran after the merge and we'd just fix the red lines. This week I wired Microsoft code analyzers plus SonarQube into my local and build pipeline for a .NET/C# service, and it felt like switching from rearview to headlights.

Catching warnings while the code is still warm means fewer bug-chasing nights, no mystery code smells, and the build itself teaches me maintainability. The feedback loop shrank from 'someone comments in PR' to 'the compiler nudges me before commit'. It keeps technical debt in check, preserves my weekend, and gives me the confidence that new code keeps the project clean.

Getting Started: Adding Analyzers

Setting up code analyzers is simpler than you might think. Here's how I added Microsoft's analyzers and SonarQube to my .NET projects:

Option 1: Add to Individual Projects

Add analyzer packages directly to your project:

dotnet add package Microsoft.CodeAnalysis.NetAnalyzers
dotnet add package SonarAnalyzer.CSharp

Option 2: Centralized Package Management (Recommended)

For solutions with multiple projects, use Directory.Packages.props to manage analyzer versions centrally. This ensures consistency across all projects.

First, enable central package management in your Directory.Build.props:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
</Project>

Then create Directory.Packages.props at your solution root:

<Project>
  <ItemGroup>
    <!-- Microsoft Code Analyzers -->
    <GlobalPackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" 
                            Version="10.0.100" />
    
    <!-- SonarQube Analyzer -->
    <GlobalPackageReference Include="SonarAnalyzer.CSharp" 
                            Version="10.16.1.129956" />
  </ItemGroup>
</Project>

Using GlobalPackageReference automatically applies analyzers to all projects in your solution—no need to add them to each .csproj individually.

Configuring Analyzer Rules

Create an .editorconfig file at your solution root to configure which rules matter and their severity:

root = true

[*.cs]
# Treat warnings as errors during build
dotnet_analyzer_diagnostic.severity = error

# Specific rules you want to enforce
dotnet_diagnostic.CA1806.severity = error  # Don't ignore method results
dotnet_diagnostic.CA2007.severity = warning # Consider ConfigureAwait
dotnet_diagnostic.S1118.severity = error   # Utility classes shouldn't have public constructors

# Rules you want to disable
dotnet_diagnostic.CA1303.severity = none   # Don't pass literals as localized params

Verify It Works

Build your project and watch the analyzers catch issues in real-time:

dotnet build

You'll immediately see warnings and errors from the analyzers. Your IDE (Visual Studio, Rider, VS Code) will also show them as you type, giving you instant feedback.

If you've been curious but haven't flipped the switch: try adding analyzers to your next build. Start with the defaults, fail the build on important rules, and see how much calmer your day feels.

← Back to blog overview