question

Prabs-7622 avatar image
0 Votes"
Prabs-7622 asked TianyuSun-MSFT commented

How to add custom ruleset in visual studio 2019?

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

dotnet-csharpvs-generaldotnet-runtime
vs2019.jpg (67.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Prabs-7622 , is your issue solved? Please let us know if there is anything that we can help here.

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon edited

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



prop.png (23.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Prabs-7622 avatar image
0 Votes"
Prabs-7622 answered cooldadtx commented

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



editor-config.jpg (115.2 KiB)
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Code analysis runs at build time. Did you build? Errors show in the error list window. Do you see anything there? Post the code you expect to see the error on. Note that analyzers were posted from fxcop so they may behave a little differently.

0 Votes 0 ·

Yes I build it..it was not showing anything in error window.
Only CS# related warnings showing not CA# related.
Do i need to install Microsoft.CodeAnalysis.FxCopAnalyzers package from nuGet?

0 Votes 0 ·

It depends upon what platform you are targeting. Starting with .NET 5 the SDK you build against already has the analyzers enabled. If you are targeting .NET Framework or any version of .NET Core prior to 5 then you need to install Microsoft.CodeAnalysis.NetAnalyzers. The FxCopAnalyzers package is not needed as it is the old rules. Refer to this doc for more information.

Once you upgrade to .NET 5+ you can remove the NuGet package as the SDK is the preferred approach.

1 Vote 1 ·
Show more comments