How to add custom ruleset in visual studio 2019?

Prabs 1 Reputation point
2021-06-24T12:30:42.74+00:00

Hi,
I have to do code analysis for my application with custom ruleset.
Is there any way to add custom ruleset in Visual Studio 2019.

In visual studio 2019, there is no option to add ruleset. Please help me on this.

109052-vs2019.jpg

Regards,
Prabs

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,636 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,300 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points
    2021-06-24T13:59:04.483+00:00

    Hello,

    For .NET Core or .NET Standard projects this is not supported at project level.

    See the Microsoft documentation, follow the steps in the first note.

    Edit See the following.

    109083-prop.png

    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 48,821 Reputation points
    2021-06-24T14:50:29.063+00:00

    Yes you can have custom rules. It is fully documented here. Note that it does not work like it did for the FxCop (pre Roslyn) analyzers. It is not integrated into the project system.

    The general preference is to put your custom rules (or exclusions) in your .editorconfig associated with the project or solution you want it applied to. Converting from a ruleset to editorconfig file is documented in the earlier link. If you really want to continue using a ruleset file then you can do so. For NF projects it works correctly. For .NET Core/Standard/5 projects you have to modify the project file. Again, refer to the docs.

    If you need to suppress certain rules then the same options existed as before. For general suppression use .editorconfig. If you need to suppress for specific lines of code then the preference is using an attribute in the code. If that isn't possible then you can continue to use a per-project suppression file.

    Finally note that the preference is to get the analyzers from the SDK for .NET 5+ and from NuGet prior to that.

    0 comments No comments

  3. Prabs 1 Reputation point
    2021-06-25T06:42:11.043+00:00

    Hi,
    I added the following custom rulesets in the Editor config file.

    dotnet_diagnostic.CA1822.severity = error
    dotnet_diagnostic.CA1800.severity = error

    When i try to run code analyzer, I did not get any errors for above mentioned rulesets.
    Could you please help me on this issue?

    109292-editor-config.jpg

    Regards,
    Prabs