Is there any way to change the thresholds for CodeAnalysis rules?
In particular, we would like our Build to fail when a method has a code complexity of more than 20. Unfortunately, rule CA1502 has a threshold of 25:
The rule reports a violation when the cyclomatic complexity is more than 25.
Can we somehow change this?
Yes, this is possible. Unfortunately, the only way to provide custom rule settings for a configurable rule is via a .fxcop
project file, which doesn't integrate terribly seamlessly with VStudio due to the order in which the rule set and project files are processed. Basically, if you want to use both a ruleset file and the project file, you will need to include a list of all rule library files in your project file with the rules disabled. You will then be able to control whether the rules are enabled or disabled via the ruleset. Once that's all set up, you can tweak the settings for the cyclomatic complexity rule using a Settings
section like the following (assuming you're OK with all the thresholds being set to 20):
<Settings>
<Rule TypeName="AvoidExcessiveComplexity">
<Entry Name="Warning Threshold">20</Entry>
<Entry Name="Information Threshold">20</Entry>
<Entry Name="Critical Warning Threshold">20</Entry>
<Entry Name="Critical Error Threshold">20</Entry>
<Entry Name="Error Threshold">20</Entry>
<Entry Name="Recommended Threshold">20</Entry>
</Rule>
</Settings>