FxCop

About

FxCop is a static code analysis tool that can analyse your .dll and .exe files and check if they following a set of rules about code style and such.
The website for the software can be found here.
Wikipedia article.
This guide is started in May 2011.

Download

FxCop is available in the Microsoft SDK for Windows 7 and .NET 4.0 Framework. It used to be available as a standalone, but Microsoft has removed this option.
Version 10.0 is available in .NET 4.0
Version 1.35 is available in earlier versions of .NET.

Installation

Installation is nice and easy. Just follow the simple wizard. For this guide I have chose the complete install.

Usage

FxCop has a GUI and a command line.

References

If an assembly (dll og exe) has any references to other assemblies, these must be added to the project. Such references could be System.Core or your own class library.
In the GUI tool however, it is possible to ignore these references and they wont affect your report. Unfortunately this option is not available in the command line and won't be in the future.

Command line interface

The tool is located in <installation folder>\FxCopCmd.exe. A list of options can be found here. All options must be appended with a colon and the parameter (if any), but with no whitespaces. A basic command looks as follows.
>FxCopCmd.exe /file:E:\My\Project\Precompiled\bin /rule:"C:\Program Files (x86)\Microsoft FxCop 1.35\Rules" /out:fxcopreport.xml
This command will look for all .dll and .exe in the folder specified in /file, hold them against the rules specified in /rule, and finally writes the output to an xml-file with the name given to the /out parameter. If you do not want to use all rule dll's in the rules folder, you can instead enter a specific dll as parameter to /rule. The same goes for /file; If you only want to analyse one dll, just select the specific dll.

"C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\DesignRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\GlobalizationRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\InteroperabilityRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\MobilityRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\NamingRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\PerformanceRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\PortabilityRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\SecurityRules.dll"
/rule:"C:\Program Files\Microsoft FxCop 1.35\Rules\UsageRules.dll"
/out:fxcopreport.xml
/summary
/file:"E:\MyProject\Model\bin\Debug\MyProject.dll"
/directory:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll"

In the above command, the flags are:
/rule
A path to the rule dll you wish to include.
/out To which file the analysis should be saved.
/summary Makes FxCop include a summary in the report.
/file The file you wish to analyse.
/directory For some reason the project file cannot remember where the System.Core.dll is located. To avoid errors you can specify the dll like this.

GUI

The GUI application can be used to create a FxCop project file. By making a project file you can specify the settings for the project one time, and just call the project file.

Setup

When you first open the FxCop GUI applikation it should look like the following.

FxCop when you first open it.


The first thing you need to do is select your .NET projects .dll-file. This is usually placed in the bin folder and is named the same as your project. To do this click on Project -> Add Targets... or the Add Target for Analysis button. Browse to the dll and click Open.

In the Targets tab you will now be able browser the elements (classes, methods, etc.) in your project. By removing the tick from an element you can exclude them from analysis.

Next you need to set your rules. To do this click on Project -> Add Rules... or on the Add Rule Library button. The rules are placed in dll's, which are located in the install folder of FxCop (ie. C:\Program Files\Microsoft FxCop 1.35\Rules). You can choose to add only the dll's you wish to include, but you will also be able to deselect rules at a more defined level next.

By selecting the Rules tab you can browse all the individual rules, and deselect the once you do not want to use.

Remember to save your project if you want to used i again. An FxCop consist of just one project file.

Analysis
When you have set up your FxCop project you can analyse your code. This is done by clicking on Project -> Analyse or on the Analyse button. Now FxCop will analyse your code and show your where the rules are broken. A popup will show a summary report, and the main windows will list all the places a rule is broken. Each item in the list will hold information about the rule, where exactly in the code the violation is, and what you should do to fix it.

The FxCop project file can be called from the command line. This way you can use it in your Continuous Integration server (such as Jenkins). The command when used by Jenkins can look like the following.

"C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe" /project:"%WORKSPACE%\fxcoprules\FxCopProject-modifiedRules.FxCop" /directory:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /out:fxcopreport.xml /summary

/project
The project file you just created.
%workspace% A sysmtem variable set by jenkins. This points to the workspace, where you can place your FxCop project file.
The other options are explained above.

A list of options for the command line can be found here.
For futher information MSDNs GUI guide can be found here.

Jenkins CI

If you are using a Continuous Integration server, such as Jenkins, to build your project, you can use the Violations plugin to display your reports.
In a Jenkins job configuration you need to add a Windows Batch command build step and enter the command that launches FxCopCmd with the correct paramters.
Note that if FxCopCmd finds violations, the Jenkins job will fail or be marked as unstable depending on the number of violations and the settings of the violations plugin.
The Violations plugin will only show a graph after a series of successful builds.
Comments