65.9K
CodeProject 正在变化。 阅读更多。
Home

在输出窗口中观察消息

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6投票s)

2017年8月11日

CPOL

1分钟阅读

viewsIcon

7035

一个方便的类,可以减少代码中的杂乱

引言

我喜欢通过在输出窗口中显示调试消息来观察代码运行。但是,在某个时候,我想减少或消除来自特定对象的消息频率。与其使用编译器指令来打开/关闭或调高/降低消息级别,我编写了这个小类,它可以被我解决方案中的任何对象使用。

它的优点在于,你可以为代码中的单个对象打开或关闭调试消息,而无需使用编译器指令来实现。

与此技巧相关的所有代码都包含在技巧的正文中。没有可下载的源代码。

代码

该类由一个 DebugMsgItem 对象列表、一个添加新的 DebugMsgItem 对象到列表的方法以及一个显示所需消息的方法组成。

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));
                }
            }
        }
    }
}

用法

通常,你会像这样从其构造函数中添加一个对象

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

另外请注意,如果你指定一个已经存在于订阅者列表中的对象,则指定的级别将应用于列表中该对象的条目,因此你可以动态地更改对象对消息的敏感度。

你将像这样触发消息

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

消息粒度有四个级别(关闭、最小、信息和完整),并且你不将对象添加到 DebugMsgs 对象中,消息将被忽略(与将对象添加到此类对象列表时指定 DebugLevel.Off 相同)。

关注点

向该类添加更多功能将非常简单,我将此作为程序员的练习留给你。

历史

2017年8月14日 - 修复了 Show 方法中的比较。

2017年8月11日 - 原始提交。

© . All rights reserved.