This is how my company has automated FxCop using MSBuild:
<!-- The directory where FxCop is installed. -->
<FxCopDirectory>C:\Program Files\Microsoft FxCop 1.36</FxCopDirectory>
<!-- The FxCop console executable.. -->
<FxCopCmd>$(FxCopDirectory)\FxCopCmd</FxCopCmd>
<Target Name="CodeAnalysis>
<!-- Once to get XML for metrics. -->
<Exec Command=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /out:$(BuildResults)\FxCop.xml /summary /verbose /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />
<!-- Once to report with the build results. -->
<Exec Command=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /out:$(BuildResults)\FxCop.html /summary /verbose /applyoutXsl:$(MSBuildTasks)\CodeAnalysisReport.xsl /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />
<!-- Update the FxCop report so that it is fully expanded by default. -->
<FileUpdate Regex="<body\s"
ReplacementText="<body onLoad="ExpandAll();" "
Files="$(BuildResults)\FxCop.html" />
</Target>
Then, you can write some C# code to consume the output file:
/// <summary>
/// Gather metrics for code analysis.
/// </summary>
private static void GatherCodeAnalysisMetrics()
{
string file = @"$(BuildResults)\FxCop.xml";
if (!File.Exists(file)) return;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(file);
System.Xml.XmlNodeList list = document.SelectNodes("//Message");
codeAnalysisWarnings = list.Count;
Console.WriteLine("Code analysis warnings: " + codeAnalysisWarnings);
}