Try our conversational search powered by Generative AI!

Johan Antila
Mar 6, 2024
  231
(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

Comments

Please login to comment.
Latest blogs
IP block for edit and admin in DXP

Why IP-blocking edit/admin? A hacker can try to guess user names and passwords to gain access to your site. This risk can be minimized in a couple ...

Daniel Ovaska | May 15, 2024

Do not upgrade to EPiServer.CMS.Core 12.21.4 without reading this!

Todays update of EPiServer.CMS.Core 12.21.4 alters default sort order in an unexpected way, when you are working with muligple languages and have...

Tomas Hensrud Gulla | May 14, 2024 | Syndicated blog

Upgrade Optimizely CMS From v11 to v12

Why Upgrade? There are many improvements implemented in version 12, but most importantly is that the whole framework is migrated from .Net Framewor...

MilosR | May 13, 2024

Configured Commerce - Infrastructure Updates Ahoy!

I'm very happy to share an important milestone - we no longer have any customers in our legacy v1 environment!  This means that the Configured...

John McCarroll | May 10, 2024