Making your debugging pretty with DotNetPretty
Special Note: This is the #1,000th Coding4Fun Blog post! Woot! Thanks for everyone's continued support and comments. And now, lets, well, look at some code! :)
There you are, at a break point, debugging and need to quickly see a variable's data. Hover over it and you see... the class name...
So we expand it and start hunting for the data we're really looking for. What I'd really like to see is...
Gordon Beeming, Microsoft Visual Studio ALM MVP, shared a project recently (okay, it's been a couple months now...) showing how you can keep in context when debugging, see the data not just the type/ToString, using the magic of the DebuggerDisplayAttribute. He not only shows off this attribute but created a project to make it even easier to use.
There's a number of cool features, but what he really shows off is how you can add this attribute to classes you don't own or even have the source for!
Because I found this awesome visualizer [TPL Dataflow Debugger Visualizer] I decided that everything while debugging could be awesome if there were more of these so I have created a GitHub project called DotNet Pretty where I plan on creating many visualizers to really try light up the debugging experience.
What is DebuggerDisplayAttribute?
In case you don't know DebuggerDisplayAttribute is used when you want to have a "pretty" representation of the properties in your class when seeing it in the debugger.
What MSDN says
Debugger display attributes allow the developer of the type, who specifies and best understands the runtime behavior of that type, to also specify what that type will look like when it is displayed in a debugger.
How do you implement them
You simple place the DebuggerDisplay Attribute on a class like below
When the break point is hit instead of you seeing the objects ToString() implementation of the method as below
which of course you could override to show a nice message if you wanted to, you will get something like below
It doesn't seem like such a big deal with 1 object but think of how easy it would be to know stuff about objects when they in a list if they each implemented this attribute. Now obviously to use the attribute like this you need to own the object so you can add the attribute and release it.
DotNet Pretty's first contribution
The first contribution to DotNet Pretty is one that was used in the training which allows you to use the DebuggerDisplay Attribute in a different way.
This time you specify the target in the attribute like below
As you can see this is just floating in a random .cs file so it doesn't have to be placed anywhere specific in the code.
What it looks like
This time instead of seeing the ToString() method
You will see
In my last post (Introduction to DotNet Pretty) I mentioned that I would be added more visualizers to DotNet Pretty soon. Today I finally managed to get around to that and added some visualizers for TFS Work Items.
3 new visualizers were added 2 debugger display visualizers and 1 debugger visualizer. The first is for Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType and just displays the Name of the Work Item Type
The next one added was for Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem and that displays the Status, Id and Title of a work item which is awesome for viewing the results of a query during debugging .
Now having the above is very useful but drilling down into fields was a nightmare so I decided to create a nice visualizer for Work Item which would show you the work item on a form as it would be inside Visual Studio. You simple click the little arrow next to the value and then click on View Work Item
and then you will notice that your life has just been changed. Any links you click on from within this window will open in the web access and not in another dialog as you might expect.
Copy the output assembly (DotNetPretty.dll) to C:\Users\[username]\Documents\[Visual Studio Version]\Visualizers, i.e.: C:\Users\GordonB\Documents\Visual Studio 2013\Visualizers
This conversation has been locked by the site admins. No new comments can be made.