Click here to Skip to main content
13,287,315 members (49,979 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.4K views
5 bookmarked
Posted 11 Aug 2017

Watching Messages in the Output Window

Rate this:
Please Sign up or sign in to vote.
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.

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:

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:

// ...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)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 11 Aug 2017
Article Copyright 2017 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid