Debug Dump Nested Variables and Big Lists in Visual Studio

Problem

I’m working on a project where I need to debug query data sets in structures I’m not familiar. So I’m setting up records in the application for known testing scenarios, creating views directly against the database, and I running my code in debug mode to set breakpoints and inspect the object structures being populated and returned.

Working in debug mode like this is difficult (one scenario is I have a List<T> of 120 and need to drill down to a 3rd level object to inspect value) as you need to painfully hover over each variable to expand and inspect. Then once you move on you lose the values.

What I want to do is dump my object structures to the console so I can later inspect and analyse the set at leisure.

Solutions

Object Exporter

Referenced:

Custom Code: Serialize to JSON

Add a reference to Newtonsoft.JSON.dll to your project.

Then create the following function:

 private static void DumpToJson(object obj)
 {
 string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
 //Console.WriteLine(json);
 System.Diagnostics.Debug.WriteLine(json);
 }

And call if from your actual code. For example:

 DumpToJson("-- Complete combinedRecipientAlleles --");
 DumpToJson(combinedRecipientAlleles);

As you can see in the following screenshot, inspecting my List<T> object is painful if I want to review all the values in the list. However, you can also see I’ve already called the first of 2 sets of calls to DumpToJson() (the first call is just a text label to identify what I’m writing out) and you can see from the Output window I have JSON being dumped to the Debug window.

Visual Studio serialize to JSON to inspect variables

Now I just copy the text in the output window into whatever editor I want to review the complete object structure). Simple!

Other Resources

The solution I went with came from here: https://www.codeproject.com/Articles/1194980/How-to-Dump-Object-for-Debugging-Purposes-in-Cshar

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>