While many of my posts cover the basics of creating analyzers and digging into the syntax, this post is going to focus on how to add existing analyzers to your projects. There has already been a great deal of work done by many people to create a wide array of analyzers and you should take advantage of those on top of creating your own analyzers.
The easiest way to add analyzers to your project is to obtain them via Nuget. A few popular analyzers include the Microsoft.AnalyzerPowerPack and StyleCop.Analyzers. Simply open up the Nuget package manager and search for the analyzer that you want to install.
Once installed, you will see the default settings that the analyzer developer decided upon for severity of the violation. Your code may end up looking something like this:
Luckily, you have full control over each and every analyzer that is provided to you and you can decide what should be reported and the severity of the violation. Under your project references, you will see a new node named
You can right click on this node and select the
Open Active Rule Set option. This will bring up a dialog that lets you configure the rules.
You'll notice an information bar at the top indicating that you are viewing the default ruleset. If you make any modifications, then it those changes will be persisted to a new file named
<projectname>.ruleset. From this window, you can change the behavior of rules. You have the following options for each rule:
|Warning||Will appear as a warning in the Visual Studio Error List window.|
|Error||Will appear as an error in the Visual Studio Error List window.|
|Info||Will appear as an information message in the Visual Studio Error List window.|
|Hidden||Analysis is still performed, but it is not reported by any Visual Studio UI. A custom UI could be created to report these diagnostics.|
|None||The rule is disabled and the diagnostic will not be run against your code.|
|Inherit||This will inherit the behavior from the rule group. There is currently no UI to set this.|
I recommend getting as many analyzers as you can get your hands on to see what value they add. After you see what analyzers are available, you can trim back the analyzers you use to match your business needs. If you have a favorite analyzer that you think other developers needs to know about, leave a comment below.