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; }
}
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()
{
DebugMsgs.Add(this, DebugLevel.Info, "MainWindow");
}
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:
DebugMsgs.Show(this, "something interesting happened", DebugLevel.Full);
DebugMsgs.Show(this, "something not so interesting happened", DebugLevel.Minimal);
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.
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.