Code analysis
Apparence
Links
Enable additional rules
Those rules are enabled by default.
Edit the project file to enable additional rules:
MyProject.csproj |
<PropertyGroup>
<!-- ... -->
<AnalysisLevel>latest-recommended</AnalysisLevel>
</PropertyGroup>
|
Editorconfig
Core options
.editorconfig |
############################### # Core EditorConfig Options # ############################### root = true # All files [*] indent_style = space insert_final_newline = true charset = utf-8 # XML project files [*.csproj] indent_size = 2 # JSON config files [*.json] indent_size = 2 # C# code files [*.cs] indent_size = 4 trim_trailing_whitespace = true |
Style configuration
.editorconfig |
############################### # Style configuration # ############################### # IDE0011: Add braces csharp_prefer_braces = when_multiline # IDE0022: Use expression body for methods csharp_style_expression_bodied_methods = when_on_single_line # IDE0160 / IDE0161: Namespace declaration preferences csharp_style_namespace_declarations = file_scoped : error |
Disable rules
.editorconfig |
############################### # Disabled rules # ############################### # lower speller rule severity to suggestion dotnet_diagnostic.VSSpell001.severity = suggestion dotnet_diagnostic.VSSpell002.severity = suggestion # IDE0046: Use conditional expression for return dotnet_diagnostic.IDE0046.severity = suggestion # IDE0058: Remove unnecessary expression value dotnet_diagnostic.IDE0058.severity = none # CA1305: Specify IFormatProvider dotnet_diagnostic.CA1305.severity = none # CA1310: Specify StringComparison for correctness dotnet_diagnostic.CA1310.severity = none # Specify CultureInfo dotnet_diagnostic.CA1304.severity = suggestion # Specify a culture dotnet_diagnostic.CA1311.severity = suggestion |
Code style analysis
# verify that all code is correctly formatted
dotnet format --verify-no-changes
# format all code
dotnet format
|
- Configuration files for code analysis rules
- An example of .editorconfig file with the default options
- Code-style rules IDExxxx
- Change encoding
.editorconfig |
Visual Studio Code integration
settings.json |
"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true,
|
Visual Studio
Set the scope of live code analysis
- Tools → Options
- Text Editor → C# → Advanced
- Run background code analysis for: Current document
- Show compiler errors and warnings for: Open documents
Roslynator
- Install the Roslynator extension in Visual Studio or Visual Studio Code
- Configure Roslynator on a user-wide basis
- vscode → Ctrl + Shift + P → enter roslynator → select Roslynator: Open Default Configuration File (.roslynatorconfig)
~/.local/share/JosefPihrt/Roslynator/.roslynatorconfig |
Roslynator CLI
# install
dotnet tool install -g roslynator.dotnet.cli
roslynator analyze \
--analyzer-assemblies \
~/.vscode/extensions/josefpihrt-vscode.roslynator-4.3.0/roslyn/common \
~/.vscode/extensions/josefpihrt-vscode.roslynator-4.3.0/roslyn/analyzers
|
![]() |
To fix the error: Could not load file or assembly 'System.Composition.AttributedModel, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
|
Code coverage
# add the XunitXML.TestLogger nuget package to the unit test project
dotnet add package XunitXML.TestLogger
# run unit tests with Coverlet data collector and log the results
dotnet test MySolution.sln --collect="XPlat Code Coverage" --logger:"xunit;LogFilePath=TestResults.xml"
# creates a TestResults.xml report and a TestResults/[guid]/coverage.cobertura.xml report
# extract the line coverage:
xmllint --xpath "string(/coverage/@line-rate)" TestResults/[guid]/coverage.cobertura.xml
sed -n -r 's/<coverage line-rate="([0-9.]+)".*/\1/p' TestResults/[guid]/coverage.cobertura.xml
# install the ReportGenerator tool
dotnet tool install -g dotnet-reportgenerator-globaltool
# get a text summary report from all the cobertura reports
reportgenerator -reports:"*/TestResults/*/coverage.cobertura.xml" -targetdir:CoverageReport -reporttypes:TextSummary
# creates CoverageReport/TextSummary.txt
# extract the line coverage:
sed -n -r 's/Line coverage: ([0-9.]+)%/\1/p' CoverageReport/Summary.txt
|
Ignore generated code
Create an .editorconfig file in a folder to not run code analysis on the files of this folder.
MyWebapi/DataAccess/.editorconfig |
[*]
generated_code = true
dotnet_analyzer_diagnostic.severity = silent
|