Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Watching Messages in the Output Window

5.00/5 (6 votes)
11 Aug 2017CPOL1 min read 6.9K  
Handy class to reduce clutter in your code

Introduction

I like to watch code run via debug messages in the output window. However, at some point, I want to reduce the frequency of or eliminate messages coming from a certain object. Instead of using compiler directives to turn messages on/off or up/down, I wrote this small class which can be used by any object in my solution.

The beauty of it is that you can turn debug messages on or off for individual objects in your code, and you don't need compiler directives to do it.

All of the code associated with this tip is in the body of the tip. There are no downloadable sources.

The Codez

The class consists of a list of DebugMsgItem objects, a method to add new DebugMsgItem objects to the list, and a method to show the desired messages.

C#
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace SQLXCommon
{
    public enum DebugLevel { Off, Minimal, Info, Full }
    public class DebugMsgItem 
    {
        public object     Parent     { get; set; }
		public string     ParentName { get; set; }
        public DebugLevel Level      { get; set; }
    }
    /// <summary>
    /// Static class that displays debug.writeline messages based on the previously 
    /// specified DebugLevel. If the parent object is not found in the list of
    /// subscribers, or if the level is less than the specified level for that object, 
    /// it is not written to the output window.
    /// </summary>
    public static class DebugMsgs
    {
        public static List<debugmsgitem> subscribers = new List<debugmsgitem>();
        public static void Add(object parent, DebugLevel level, string parentName = "")
        {
            var found = subscribers.FirstOrDefault(x=>x.Parent == parent);
            if (found == null)
            {
                subscribers.Add(new DebugMsgItem() 
                { 
                   Parent     = parent, 
                   Level      = level, 
                   ParentName = parentName 
                });
            }
            else
            {
                found.Level = level;
            }
        }
        public static void Show(object obj, string msg, DebugLevel level)
        {
            var found = subscribers.FirstOrDefault(x=>x.Parent == obj);
            if (found != null)
            {
                if ((int)level <= (int)found.Level)
                {
                    Debug.WriteLine(string.Concat(found.ParentName, 
                                    string.IsNullOrEmpty(found.ParentName.Trim()) ? " - " : "", msg));
                }
            }
        }
    }
}

Usage

Ostensibly, you would add an object from its constructor, like so:

C#
public MainWindow()
{
    // specifying DebugLevel.Info will allow all messages except those 
    // marked as DebugLevel.Full
    DebugMsgs.Add(this, DebugLevel.Info, "MainWindow");
    // ...more code
}

Also take note that if you specify an object that's already in the list of subscribers, the specified level will be applied to the object's item in the list, so you could change the object's sensitivity to messages on the fly.

And you would trigger messages like so:

C#
// ...more code
// given the example shown above, this message will be displayed 
DebugMsgs.Show(this, "something interesting happened", DebugLevel.Full);
// but this message will NOT be displayed
DebugMsgs.Show(this, "something not so interesting happened", DebugLevel.Minimal);
// ...more code

There are four levels of message granularity (off, minimal, info, and full), and you don't add the object to the DebugMsgs object at all, the message will be ignored (same as specifying a DebugLevel.Off when you add your object to this class's list of objects.

Points of Interest

It would be a simple matter to add more functionality to the class, and I leave that as an exercise for the programmer.

History

14 Aug 2017 - Fixed the comparison in the Show method..

11 Aug 2017 - Original submission.

License

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