Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Few Tips on Customizing Debugging Window View in Visual Studio

, 29 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A few tips on customizing debugging windows

In this post, I am going to discuss a few tips on customizing debugging windows. This may be very helpful during debugging of an application. While debugging, you may want to simplify debug window or you may want to clean up all the unnecessary fields that are not important during debugging from debugging windows. Here are a few tips for customization of debug window.

lightbulb Use DebuggerBrowsable attribute to customize the debugging windows:

2

lightbulb Use DebuggerDisplay attribute to customize the debugging display.

1

To use the above attributes, you have to use System.Diagnostics namespace.

1. Using DebuggerBrowsable Attributes

If you want to customize the view on debugger window for any properties during debugging, you can easily do it using DebuggerBrowsable attributes. You can apply these attributes for any properties, fields or for Indexer. DebuggerBrowsable attributes constructor takes DebuggerBrowsableState as argument. DebuggerBrowsableState is used to provide the instruction to debugger about how it is going to be displayed in the debugger window.

We can provide three states for DebuggerBrowsable attributes:

  1. Collapsed: If we set DebuggerBrowsableState as collapsed, then the debugger window will show the element as collapsed. It’s the default behavior.
  2. Never: It will never show the element in debugging window.
  3. RootHidden: It will hide the root elements and display the all child items as expanded view.

You can read the complete definition of these DebuggerBrowsableState at MSDN.

Now I am going to demonstrate the use of this DebuggerBrowsable attributes and DebuggerBrowsableState using an example.

Before starting, let’s consider having the following code block:

namespace DebuggerDemo
{
    /// <span class="code-SummaryComment"><summary>
</span>    /// Student Info Demo Class
    /// <span class="code-SummaryComment"></summary>
</span>    class Program
    {
        /// <span class="code-SummaryComment"><summary>
</span>        /// Mains the specified args.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="args">The args.</param>
</span>        static void Main(string[] args)
        {
            List<Student> student = new List<Student>();
            student.Add(new Student { Roll = 1, Name = "Abhijit", Marks = 87, 
		Addresses = new Address { Address1 = "add1", Address2 = "add2" } });
            student.Add(new Student { Roll = 2, Name = "Abhishek", Marks = 41, 
		Addresses = new Address { Address1 = "add3", Address2 = "add4" } });
            student.Add(new Student { Roll = 3, Name = "Rahul", Marks = 67, 
		Addresses = new Address { Address1 = "add5", Address2 = "" } });
            student.Add(new Student { Roll = 4, Name = "Sunil", Marks = 91, 
		Addresses = new Address { Address1 = "add11", Address2 = "add122" } });
            student.Add(new Student { Roll = 5, Name = "Atul", Marks = 71, 
		Addresses = new Address { Address1 = "add12", Address2 = "add222" } });
            student.Add(new Student { Roll = 6, Name = "Kunal", Marks = 71, 
		Addresses = new Address { Address1 = "add12", Address2 = "add222" } });
        }
    }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Student Class
    /// <span class="code-SummaryComment"></summary>
</span>    class Student
    {
        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the roll.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The roll.</value>
</span>        public int Roll { get; set; }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the name.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The name.</value>
</span>        public string Name { get; set; }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the marks.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The marks.</value>
</span>        public int Marks { get; set; }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the addresses.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The addresses.</value>
</span>        public Address Addresses { get; set; }
    }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Address of Students
    /// <span class="code-SummaryComment"></summary>
</span>    class Address
    {
        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the address1.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The address1.</value>
</span>        public string Address1 { get; set; }

        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets or sets the address2.
        /// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><value>The address2.</value>
</span>        public string Address2 { get; set; }
    }
}

Now, first let’s see how the normal debugging window behaves. Just put a breakpoint at the end of main method and try to explore the debugging window. You will get a debugging window as the below picture, which is the expected debugging window view.

4

In the above picture, you can see that we are having 6 student objects and each one having a different value. As Addresses is a different class and used as properties with multiple value, hence it is in the collapsed mode.

Now, I want to see all the addresses along with all other properties with expanded mode and also want to hide the Marks properties. To achieve the above requirement, we have to add DebuggerBrowsable attributes for the Marks and Addresses properties in the Student class.

5

Now if you put the breakpoint in the same location and explore the debugging window, you will find the debugging window view as the below picture:

6

So, from the above picture, you can easily identify the changes in the debugging window view.

2. Use DebuggerDisplay Attribute

Here is the second tip. By using DebuggerDisplay attributes, you can define how a class or field will be displayed in the debugger window. Using DebuggerDisplay, you can change the debugger window message and variables that you want to display.

If you consider the above code sample and debug the application, by default, you will get the below snaps of debugging:

7

Here, for each student object, you are getting NameSpace.ClassName as display message by default. Now we can customize the display using DebuggerDisplay attributes. DebuggerDisplay attributes constructors take display name as arguments or you can pass the named parameter that you want to display over there.

8

After making the above changes, if you run the same code you will find that custom display message with proper value of parameter that you have given in debuggerdisplay attributes.

9

Keys While using DebuggerDisplay, you have to make sure that you are giving the proper field name as argument within { }. Otherwise, you will get message like below:

10

Summary In this blog post, I have explained how we can customize the debugging window’s view during debugging of our application using DebuggerBrowsable and DebuggerDisplay attributes. This is quite helpful while you are debugging a complex object and you want to make your debug window very simple.

Hope the above tips will help you in customizing debugging windows.

Filed under: General, Tips and Tricks, Visual Studio

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband
 
Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMark Lemke13-Aug-12 11:12 
GeneralMy vote of 5 PinmvpAl-Farooque Shubho3-Sep-10 5:10 
GeneralRe: My vote of 5 PinmvpAbhijit Jana12-Sep-10 22:02 
GeneralMy vote of 5 PinmentorKunalChowdhury31-Aug-10 2:11 
GeneralRe: My vote of 5 PinmvpAbhijit Jana12-Sep-10 22:01 
QuestionWhat version? PinmemberStefan6330-Aug-10 23:02 
AnswerRe: What version? PinmvpAbhijit Jana31-Aug-10 0:29 
GeneralRe: What version? PinmemberStefan6331-Aug-10 0:39 
GeneralRe: What version? PinmvpAbhijit Jana31-Aug-10 0:41 
GeneralMy vote of 5 PinmemberS M P30-Aug-10 1:39 
GeneralRe: My vote of 5 PinmvpAbhijit Jana30-Aug-10 2:50 
GeneralMy vote of 5 Pinmemberlinuxjr29-Aug-10 7:21 
GeneralRe: My vote of 5 PinmvpAbhijit Jana29-Aug-10 8:07 
GeneralMy vote of 5 PinmemberAbhinav S29-Aug-10 6:47 
GeneralRe: My vote of 5 PinmvpAbhijit Jana29-Aug-10 8:06 
GeneralMy vote of 5 PinmvpAbhishek Sur29-Aug-10 6:23 
GeneralRe: My vote of 5 PinmvpAbhijit Jana29-Aug-10 8:06 
GeneralMy vote of 5 PinmvpNishant Sivakumar29-Aug-10 6:15 
GeneralRe: My vote of 5 PinmvpAbhijit Jana29-Aug-10 6:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 29 Aug 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid