A Look Into Roslyn Analyzers

Kendrick
Bina Nusantara IT Division
4 min readDec 30, 2021
Illustration of Source Code Analysis

Roslyn analyzer is a static code analysis tool for the .NET Compiler Platform. Roslyn analyzer identifies issues such as style, performance, and code smells within C# or Visual Basic source code. Roslyn analyzer inspects the code when the file is opened. A list of popular analyzers can be found here.

Installing Roslyn Analyzer on Visual Studio

Roslyn analyzer can be installed on Visual Studio as NuGet Package or Extension NuGet Package.

Installing as NuGet Packages

Visual Studio has a built-in NuGet Package Manager which allows us to add a NuGet Package in multiple ways to the solution. We can access NuGet Package Manager from the Tools menu from the Menu Bar.

There are three options in the NuGet Package Manager. For convenience, we can use “Manage NuGet Packages for Solution…” to search for a NuGet Package using the in-built explorer.

After clicking “Manage NuGet Packages for Solution” the following window will appear.

In this windows we can use the search bar in the Browse tab to search for the NuGet Packages:

For example, we want to install “SonarAnalyzer”. After searching it using the search bar. We can click on the results to show the description and proceed to installation.

To install the package, we need to click select the projects where we need the NuGet Packages to be installed and click on the “Install” button to start installing.

During installation click on “OK” during the “Preview Changes” window. You can tick “Do not show this again” for this window to not appear again in the next package installation.

Installing as Visual Studio Extension

Visual Studio Extension Installer (.vsix) file can be downloaded from here. We can install the extension by directly opening the file.

We can also download and install the extension directly in Visual Studio using the “Manage Extension” Feature from the Extensions menu.

In this window, we can use the search bar to search for the analyzer and click select the needed extension to be downloaded.

Notable differences of NuGet Packages and Extension

Scope

If the analyzer is installed as NuGet Packages then the analyzer only works on the project/solution which has the NuGet Packages installed. Meanwhile, if the analyzer is installed as an Extension, the analyzer will work universally across all projects.

Severity Configuration

If the analyzer is installed as NuGet Packages, the severity of the issues can be configured using the EditorConfig file. Meanwhile, if the analyzer is installed as an Extension then the severity cannot be configured.

Issues severity

Roslyn analyzer runs static code analysis and reports the issues based on the severity.

By default here is the common category of severity in Visual Studio :

Configuring severity of issues

NOTE: Only works if the analyzer is installed as NuGet Package.

By using the EditorConfig file we can configure the severity of the issues.

First, create a file named “.editorconfig” file in the solution’s root if it does not exist.

In the “.editorconfig” file, add the attribute containing the format of the file for the analyzer. For the C# code, the attribute is [*.cs] (any file with the extension of .cs). Then add “dotnet_diagnostic.ISSUE_CODE.severity = SEVERITY_NAME” for each issue severity to be configured. Replace ISSUE_CODE for issue code and SEVERITY_NAME for new severity classification.

Here is an example of the .editorconfig file

--

--