Try our conversational search powered by Generative AI!

Johan Antila
Mar 6, 2024
(1 votes)

When Visual Studio Code Metrics fail

Visual Studio Code Metrics- that won't show up.

At Optimizely Expert Services, we are often times asked to do code reviews and assessments of customer solutions and these typically involve using static code analysers and reading code bases. One good tool for finding complex parts of a solution is the Code Metrics tool in Visual Studio that gives you an indication of hot spots in the code base. For example, it can show you the complexity of the code, generate something it calls Maintainability Index, It can list lines of code and display what of this is executable code, the latter is especially handy for projects and namespaces where you have your views. Unfortunately, there's been a few times where I have happened upon a bug in the Visual Studio Roslyn engine that stops the Code Metrics from rendering in the Code Metrics Window. Instead of listing the namespaces and calculated metrics, the window is just blank. In vain, I have struggled to find the reason behind this and to fix it for the specific solution but I have come up empty handed. When it happened again this time and I went searching for a solution, one of the pages I came across mentioned that there was an automated process calculating the metrics and this led me to investigate if there was a command line tool to do this and perhaps, this would work when the built one wouldn't. It turns out that indeed, Microsoft has created such a tool called Microsoft.CodeAnalysis.NetAnalyzers You can either add a package reference to your project to be able to target it using msbuild or compile a stand alone command line tool called to do this calculation in a command shell. As the nuget package solution didn't work for me, I opted for the latter solution with a command line tool and followed this guide.

With the tool compiled, start up a Visual Studio Command Prompt, and run it to generate an xml file: 

metrics.exe /p:project myproject.csproj /out:report.xml

This generates an XML file with the code metrics in it but that's not the Excel output that the built in tool generates. Now what? Well you have to parse the xml file some way so I opted to parse it to json and iterate over it using a serialized version of the file.

I wrote a small tool to do just that, it takes the xml file and outputs a csv file that you can import in Excel. You can find it over at GitHub.

One final note, to be able to parse the json in a compile time fashion, you need to catch the json content after it has been parsed from XML, copy it and use Visual Studio -> Edit -> Paste Special -> Paste JSON as classes to get classes that you now can use in the code.

Mar 06, 2024


Please login to comment.
Latest blogs
The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024

What's new in Language Manager 5.3.0

In Language Manager (LM) version 5.2.0, we added an option in appsettings.json called TranslateOrCopyContentAreaChildrenBlockForTypes . It does...

Quoc Anh Nguyen | Apr 15, 2024

Optimizely Search & Navigation: Boosting in Unified Search

In the Optimizely Search & Navigation admin view, administrators can set a certain weight of different properties (title, content, summary, or...

Tung Tran | Apr 15, 2024

Optimizely CMS – Getting all content of a specific property with a simple SQL script

When you need to retrieve all content of a specific property from a Page/Block type, normally you will use the IContentLoader or IContentRepository...

Tung Tran | Apr 15, 2024

Join the Content Recommendations Work Smarter webinar May 8th 16.00-16.45 CET with expert Aidan Swain

Learn more about Content Recommendations, with Optimizely’s very own Senior Solutions consultant, Aidan Swain . He will discuss best practices and...

Karen McDougall | Apr 12, 2024