Calculate Code Metrics automatically for all your applications and see the trend

by Ewald Hofman 7. October 2009 04:32

In Visual Studio 2008, Microsoft has added the code metrics, which gives you information about the quality and maintainability of the application. Unfortunately you can not easily start the code metrics automically, and you can not integrate it from Team Build.

There is NDepend, which has the ability to integrate with team build, that gives you access to the same code metrics. But NDepend does not have a repository so you can build a trend of the metrics for all your applications stored in your TFS source control.

So I created a solution which works around these problems.

The idea of the application is quite simple:

  1. Download all sources in TFS to the local drive
  2. One by one open the solution
  3. For each solution calculate the code metrics
  4. Store the results of the code metrics in a database for reporting and trend analysis

It is do-able, but it was a long story to get a solution that works. I like to share the solution with you.


  • EntLib 4.0
  • SQL Server 2005
  • Visual Studio 2008 Development Edition or Team Suite
  • Team Foundation Server 2008
  • Microsoft Excel 2003 (not tested on Excel 2007)

To start the application, do the following:

  1. Download the sources at the bottom of the page.

  2. Open the solution

  3. Deploy the CodeMetricsDatabase project to the SQL Server of your choice

  4. Set the connection string to the SQL Server the database project is deployed to

    1. Open the app.config

    2. Change the CodeMetrics connection string in the connectionStrings section

  5. Start the application with the following parameters:

    1. TFS server name (for example http://tfsserver:8080)

    2. Start node in TFS source control (for example $/MyTeamProject/MySolution)

    3. The local path where the sources are stored (for example c:\CodeMetricsCache)

    4. And optional you can pass the Team Project you are only interested in (289.11 kb)


To be able to see the trend of the metrics, I have created a report which you can install on the TFS Reporting site. To install the report, you must add a datasource called "CodeMetrics". This datasource must have the connection information to the database where you store the code metrics.

The second step is to update the rdl below. You can now see the trend of the code metrics of all your applications.

Code Metrics Trend.rdl (34.71 kb)


Reporting | VSTS 2008

Comments are closed

Powered by BlogEngine.NET
Theme by Mads Kristensen


Widget Statistics not found.

There is an error in XML document (0, 0).X

Recent comments

Comment RSS