Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

System.Diagnostics Useful Actions

Rate me:
Please Sign up or sign in to vote.
4.79/5 (17 votes)
5 Apr 2017CPOL2 min read 18K   17   5
Useful Actions for debugger in System.Diagnostics

Introduction

The namespace System.Diagnostics provides a set of attributes and classes to interact with the system process, events managers, performance counts, etc. This namespace can help us too in debugging job.

Let’s review the useful actions inside of System.Diagnostics namespace.

DebbugerDisplay Attribute

DebuggerDisplay attribute drives the string format with debug screen to show the value of: class, properties or fields.

For this same task, is best known override ToString method, but using DebbugerDisplay attribute is a better choice, because this not only modifies the data structure, only interacts with Visual Studio debbuger screen. Override ToString method for only this purpose can create problems because many actions in .NET take this value for default, for example bindings in WPF.

This attribute supports delegates, properties, fields and assemblies.

Example:

C#
[System.Diagnostics.DebuggerDisplay("{ID} - {Model}- {Manufacturer} - {ProductionDate}")]
public class Car
{
    public int      ID             { get; set; }
    public string   Model          { get; set; }
    public string   Manufacturer   { get; set; }
    public DateTime ProductionDate { get; set; }
}

Image 1

DebuggerHidden Attribute

DebuggerHidden attribute prevents the compiler stop in constructors, methods, properties and indexers declarations.

In mentioning this latter area, my comment might sound lightweight, but in practice, this can save time push key F11 in debugging.

Example:

C#
[System.Diagnostics.DebuggerHidden]
public static List<Car> GetData()
{
    var result = new List<Car>()
    {
        new Car{ ID = 1, Manufacturer = "Ford",   
        Model = "Mustang", ProductionDate = DateTime.Today },
        new Car{ ID = 2, Manufacturer = "Nissan", 
        Model = "Micra"  , ProductionDate = DateTime.Today }
    };

    return result;
}

Debugger.Launch

Occasionally, we can’t debug the code of a library, service, etc., because it isn’t accessible or we can’t add project to our solution. In this case, we can use the Debugger.Launch() method and Visual Studio opens a debug window and we can debugger its code.

Image 2

When executed, the line Systen.Diagnostics.Debbuger.Launch() open a MessageBox with the instance of Visual Studio Debugger Options:

Image 3

In this window, we can choose if we open a new instance of Visual Studio (all versions) or if we re-use an existing instance.

And we can debug the code:

Image 4

Conditional Attribute

Conditional attribute allows to indicate a condition to methods so that the compiler executes or does not execute its content.

We can use with the precompiler sentences as DEBUG.

C#
static void Main(string[] args)
{
    DebugMethod();
    Console.Read();
}

[System.Diagnostics.Conditional("DEBUG")]
public static void DebugMethod()
{
    Console.WriteLine("Execute Debug Method");
}

Will only run if the Solutions Configurations is Debug.

It doesn’t exist a condition for RELEASE, therefore we will use a define directives.

Define directives is another way to use System.Diagnostics.Conditional:

C#
using System;
namespace SystemDiagnosticsUsefulActions
{
    class Program
    {
        static void Main(string[] args)
        {
            ReleaseMethod();

            Console.Read();
        }

        [System.Diagnostics.Conditional("RELEASE_MODE")]
        public static void ReleaseMethod()
        {
            Console.WriteLine("Execute Release Method");
        }
    }
}

Conclusion

These are the useful tips and traps of System.Diagnostics. They are become very practical in many cases and I hope you find it helpful.

License

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


Written By
Software Developer (Senior) Cecabank
Spain Spain
MVP C# Corner 2017

MAP Microsoft Active Professional 2014

MCPD - Designing and Developing Windows Applications .NET Framework 4
MCTS - Windows Applications Development .NET Framework 4
MCTS - Accessing Data Development .NET Framework 4
MCTS - WCF Development .NET Framework 4

Comments and Discussions

 
PraiseUseful Pin
Angelo L17-Apr-17 8:33
Angelo L17-Apr-17 8:33 
GeneralMy vote of 5 Pin
Gaston Verelst11-Apr-17 1:21
Gaston Verelst11-Apr-17 1:21 
Nice and short.
Here you can find some more: https://msdev.pro/2011/10/06/rediscovered-the-debug-attributes/

kr,
Gaston
GeneralRe: My vote of 5 Pin
Juan Francisco Morales Larios11-Apr-17 4:16
Juan Francisco Morales Larios11-Apr-17 4:16 
QuestionThanks for the reminder Pin
Murray Hertz7-Apr-17 7:18
Murray Hertz7-Apr-17 7:18 
AnswerRe: Thanks for the reminder Pin
Juan Francisco Morales Larios7-Apr-17 20:57
Juan Francisco Morales Larios7-Apr-17 20:57 

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

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