Visitar URL original
Enable .NET code analysis with AnalysisMode Recommended by anyingiit · Pull Request #6313 · NLog/NLog · GitHub
Skip to content

Enable .NET code analysis with AnalysisMode Recommended - #6313

Open
anyingiit wants to merge 2 commits into
NLog:mainfrom
anyingiit:build/enable-net-analyzers
Open

anyingiit wants to merge 2 commits into
NLog:mainfrom
anyingiit:build/enable-net-analyzers

Conversation

@anyingiit

Copy link
Copy Markdown

Requested by winshyon

Description

Enables the built-in .NET code analyzers for the projects in src/, as proposed in the issue:

<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Recommended</AnalysisMode>

The properties are added next to TreatWarningsAsErrors in all six src projects (NLog, NLog.OutputDebugString, NLog.RegEx, NLog.Targets.AtomicFile, NLog.Targets.GZipFile, NLog.WindowsEventLog). Since these projects build with TreatWarningsAsErrors, turning the analyzers on without tuning breaks the build (464 errors for src/NLog across its target frameworks). .editorconfig therefore gets:

  • The four entries from the issue (CA1001, CA1848 → suggestion; CA1707, IDE0130 → none).
  • Public API rules → none / suggestion: CA1000, CA1069, CA1710, CA1711, CA1716 (none, e.g. 46 hits for identifiers like Error/Event/Default) and CA1067, CA1068, CA1725 (suggestion). Fixing them would mean breaking or behavior-relevant API changes.
  • Suggested API not available on all targets (net35 / netstandard2.0) → suggestion: CA1840, CA1858, CA1865, CA2249, CA2263.
  • Remaining code quality rules → suggestion for now, so they still show in the IDE and can be cleaned up in separate, focused PRs: CA1305, CA1309, CA1806, CA1822, CA1830, CA1838, CA1859, CA1861, CA2101, CA2208.

No source code is changed, so the PR only affects build diagnostics. Each .editorconfig line carries the rule title as a comment, so it's easy to raise individual rules back to warning once they are fixed. The test projects are not included because they don't build with TreatWarningsAsErrors, and the Recommended mode reports roughly 1000 extra warnings there (mostly CA1707, CA1305, CA2201). I'm happy to add them in a follow-up if you want.

Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to close it, no hard feelings at all 🙂

Related issue

Closes #5916

Checklist

  • Tests pass locally (.NET SDK 10.0.401, Linux)
    • Analyzers on without the .editorconfig tuning: dotnet build src/NLog/NLog.csproj -c Release fails with 464 errors; the other five src projects fail as well (they build NLog first, and NLog.RegEx / NLog.Targets.AtomicFile additionally hit CA2263 / CA2101).
    • With this PR: dotnet build src/<project>/<project>.csproj -c Release --no-incremental succeeds for all six src projects with 0 errors and no CA/IDE warnings. The only warning left is NETSDK1210, which also appears on dev. The same holds for src/NLog with /p:targetFrameworks="net46;net45;net35;netstandard2.0;netstandard2.1" as in build.ps1.
    • dotnet test ./tests/NLog.UnitTests/ --framework net10.0 --configuration release: Passed 2970, Skipped 8, Failed 0
    • Not run: the Windows-only steps (CheckSourceCode, net462/net35 test runs, build.ps1 pack).
  • CHANGELOG.md is updated (if applicable) — n/a (release notes are maintained by the maintainers)
  • Documentation is updated (if applicable) — n/a (build configuration only)

🤖 Generated with Claude Code

Enable EnableNETAnalyzers with AnalysisLevel latest and AnalysisMode
Recommended for the projects in src (all built with TreatWarningsAsErrors),
and tune .editorconfig severities so the build stays warning-free:
public API rules that would require breaking changes are disabled, and
the remaining code quality rules are reported as suggestions for now.
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 30f7b070-df24-4a66-933c-cc172f70aaec
Commits

Reviewing files that changed from the base of the PR and between 5aadab3 and 3fb57c3.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Six projects now enable .NET analyzers with the latest analysis level and Recommended mode. The .editorconfig file sets severities for selected analyzer rules.

Changes

.NET analyzer enablement

Layer / File(s) Summary
Enable analyzers and set rule severities
.editorconfig, src/NLog/NLog.csproj, src/NLog.OutputDebugString/NLog.OutputDebugString.csproj, src/NLog.RegEx/NLog.RegEx.csproj, src/NLog.Targets.AtomicFile/NLog.Targets.AtomicFile.csproj, src/NLog.Targets.GZipFile/NLog.Targets.GZipFile.csproj, src/NLog.WindowsEventLog/NLog.WindowsEventLog.csproj
The six projects enable .NET analyzers with the latest analysis level and Recommended mode. .editorconfig assigns severities to selected analyzer rules.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Minimal · previous review 5aada

No actionable merge-blocking risk remains in the reviewed analyzer configuration changes.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly summarizes the main change: enabling .NET code analysis with Recommended analysis mode.
Description check Passed The description explains the analyzer settings, severity adjustments, project scope, and reported validation results.
Linked Issues check Passed Issue #5916 requests enabling .NET analyzers with EnableNETAnalyzers=true, AnalysisLevel=latest, and AnalysisMode=recommended, and permits .editorconfig severity adjustments. The reviewed `src…
Out of Scope Changes check Passed The reported changes are limited to analyzer settings in the six src/ projects and .editorconfig. The additional severity settings tune analyzer diagnostics for public APIs, target-framework compa…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Full details: Linked Issues check

Explanation

Issue #5916 requests enabling .NET analyzers with EnableNETAnalyzers=true, AnalysisLevel=latest, and AnalysisMode=recommended, and permits .editorconfig severity adjustments. The reviewed src/NLog/NLog.csproj and src/NLog.RegEx/NLog.RegEx.csproj contain these settings. The PR summary reports the same changes in all six src/ projects. .editorconfig includes the four example rule severities from the issue. The author reports successful Release builds for all six projects.

Full details: Out of Scope Changes check

Explanation

The reported changes are limited to analyzer settings in the six src/ projects and .editorconfig. The additional severity settings tune analyzer diagnostics for public APIs, target-framework compatibility, and code quality. They support enabling Recommended analysis under TreatWarningsAsErrors. No unrelated changes appear in the supplied PR summary.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (7 skipped: 7 unsupported.)

Finishing Touches

🧪 Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the rules today,
New analyzer settings join the way.
Six projects heed the latest guide,
While severity notes stand beside.
I twitch my nose and hop along,
The config changes make my song.

ℹ️ Recent review info

⚙️ Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 55619a31-c8ba-40b2-8f63-3d3a0c545009
📥 Commits

Reviewing files that changed from the base of the PR and between a38a6d3 and 5aadab3.

📒 Files selected for processing: 7

  • .editorconfig
  • src/NLog.OutputDebugString/NLog.OutputDebugString.csproj
  • src/NLog.RegEx/NLog.RegEx.csproj
  • src/NLog.Targets.AtomicFile/NLog.Targets.AtomicFile.csproj
  • src/NLog.Targets.GZipFile/NLog.Targets.GZipFile.csproj
  • src/NLog.WindowsEventLog/NLog.WindowsEventLog.csproj
  • src/NLog/NLog.csproj

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@sonarqubecloud

Copy link
Copy Markdown

@snakefoot

Copy link
Copy Markdown
Contributor

My goal for #5916 is to incrementally fix all relevant issues, and then at the very end put the shackles on (enable the validation by default with minimal suppression)

@304NotModified

Copy link
Copy Markdown
Member

FYI we renamed dev to main. So if you need to merge with upstream, use main :)

The PR has been retargeted to main

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.60%. Comparing base (e157caf) to head (3fb57c3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6313      +/-   ##
==========================================
- Coverage   73.62%   73.60%   -0.02%     
==========================================
  Files         341      341              
  Lines       20795    20795              
  Branches     5282     5282              
==========================================
- Hits        15310    15306       -4     
- Misses       3829     3831       +2     
- Partials     1656     1658       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@304NotModified

Copy link
Copy Markdown
Member

FYI the CI has been updated (see #6328) and I added a merge commit to this branch.

Also good to know, we gonna move to SonarCloud automatic analysis, and then the Roslyn warnings are not visible in SonarCloud anymore. A possible solution is sending it to GitHub Code Quality, see NLog/NLog.Extensions.Logging#896

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable Dotnet Code Analysis by default

3 participants