(一)设计目的
对于C#的控制台程序,通常我们的输出是在调试控制台,但是C#会自动输出控制台程序的加载信息,这些并不是我们想要关注的信息,所以为了更直观地查看我们的程序在控制台中输出的信息,我们可以通过自定义控制台输出函数结合控制台筛选器来实现这个目的。
自定义控制台输出函数是基于System.Diagnostics.Debug中的Write和WriteLine方法进行简单的自定义,并非是完全自主实现控制台输出方法。也就是将内置的这两个方法进行封装,给我们的调试信息加上一些特殊标记,这样我们就可以通过调试控制台的筛选器结合特殊标记筛选控制台信息,从而实现控制台仅显示具有该特殊标记的信息。
(二)代码示例(C#)
public static class MyPrint
{
/// <summary>
/// 标记,默认为"<b>(MyPrint)</b>"
/// </summary>
public static string mFlag
{
get { return flag; }
set { if (value != null) flag = value; }
}
private static string flag;
private static bool isFirst;
static MyPrint()
{
flag = "(MyPrint)";
isFirst = true;
}
//该特性使得调试时直接跳过该方法
[System.Diagnostics.DebuggerStepThrough]
public static void WriteLine(object str)
{
if (str == null) System.Diagnostics.Debug.WriteLine($"{flag} Null");
else if (str.Equals(string.Empty)) System.Diagnostics.Debug.WriteLine("");
else System.Diagnostics.Debug.WriteLine($"{flag} " + str.ToString());
}
[System.Diagnostics.DebuggerStepThrough]
public static void WriteLine()
{
System.Diagnostics.Debug.WriteLine("");
}
[System.Diagnostics.DebuggerStepThrough]
public static void Write(object str)
{
if (isFirst)
{
if (str == null) System.Diagnostics.Debug.Write($"{flag} Null");
else System.Diagnostics.Debug.Write($"{flag} " + str.ToString());
isFirst = false;
}
else System.Diagnostics.Debug.Write(str);
}
}
(三)测试
(1)MyPrint测试
①WriteLine()测试
// MyPrint.WriteLine测试
MyPrint.WriteLine("Hello world!");
MyPrint.WriteLine("My name is Jack.");
②Wirte()测试
// MyPrint.Write测试
string[] v_strs1 = { "Hello ", "world." };
foreach (string str in v_strs1)
{
MyPrint.Write(str);
}
string[] v_strs2 = { "My ", "name ", "is ", "Jack." };
foreach (string str in v_strs2)
{
MyPrint.Write(str);
}
③WriteLine()和Write()联调测试
MyPrint.WriteLine("Hello Jack.");
string[] v_strs1 = { "Hello ", "world." };
foreach (string str in v_strs1)
{
MyPrint.Write(str);
}
MyPrint.WriteLine("");
string[] v_strs2 = { "My ", "name ", "is ", "Jack." };
foreach (string str in v_strs2)
{
MyPrint.Write(str);
}
MyPrint.WriteLine("My name is Mary.");
(2)对比测试
通过System.Diagnostics.Debug与MyPrint中的函数Write和WriteLine实现相同的逻辑并对输出结果进行对比。以下三个测试基于System.Diagnostics.Debug实现。
①WriteLine()测试
// WriteLine测试
System.Diagnostics.Debug.WriteLine("Hello world!");
System.Diagnostics.Debug.WriteLine("My name is Jack.");
②Wirte()测试
// Write测试
string[] v_strs1 = { "Hello ", "world." };
foreach (string str in v_strs1)
{
System.Diagnostics.Debug.Write(str);
}
string[] v_strs2 = { "My ", "name ", "is ", "Jack." };
foreach (string str in v_strs2)
{
System.Diagnostics.Debug.Write(str);
}
③WriteLine()和Write()联调测试
System.Diagnostics.Debug.WriteLine("Hello Jack.");
string[] v_strs1 = { "Hello ", "world." };
foreach (string str in v_strs1)
{
System.Diagnostics.Debug.Write(str);
}
System.Diagnostics.Debug.WriteLine("");
string[] v_strs2 = { "My ", "name ", "is ", "Jack." };
foreach (string str in v_strs2)
{
System.Diagnostics.Debug.Write(str);
}
System.Diagnostics.Debug.WriteLine("My name is Mary.");
(四)测试总结
根据上述测试结果可见,在忽略输出结果中的标记"(MyPrint)"的前提下,MyPrint和内置的System.Diagnostics.Debug对于WriteLine和Write方法的控制台输出信息相同。
(五)使用示例
示例代码如下,输出结果如图7和图8所示:
//示例代码
MyPrint.WriteLine("【Example】");
MyPrint.WriteLine("-----------------------------");
string[] v_fruits = { "Apple", "Orange", "Banana", "Strawberry", "Peach" };
string[] v_prices = { "10$", "20$", "15$", "50$", "100$" };
for (int i = 0; i < 5; i++)
{
if (i < 4) MyPrint.Write($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}],");
else MyPrint.Write($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}]");
}
MyPrint.WriteLine();
MyPrint.WriteLine("-----------------------------");
图8所示并不需要每次调试控制台程序都在筛选器输入标记来筛选信息,这个筛选器会缓存当前输入的筛选标记,所以后续只需要运行调试控制台程序编辑即可自动按照图8所示的方式显示控制台输出信息,但是值得注意的是,这个方法可能会出现错误筛选掉有效信息的情况,这种情况之一的代码如下:
//示例代码
MyPrint.WriteLine("【Example】");
MyPrint.WriteLine("-----------------------------");
string[] v_fruits = { "Apple", "Orange", "Banana", "Strawberry", "Peach" };
string[] v_prices = { "10$", "20$", "15$", "50$", "100$" };
for (int i = 0; i < 5; i++)
{
if (i < 4) MyPrint.Write($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}],");
else MyPrint.Write($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}]");
MyPrint.WriteLine();//在原有示例代码基础上添加了这一段代码
}
MyPrint.WriteLine();
MyPrint.WriteLine("-----------------------------");
如图9和图10所示,我们的有效信息被错误筛选掉了一部分,这是因为筛选器是按行识别的,若该行输出信息中没有指定的标记则会被过滤掉,所以这种情况建议改用WriteLine输出。
通过WriteLine实现的代码如下,输出结果如图11和图12:
//示例代码
MyPrint.WriteLine("【Example】");
MyPrint.WriteLine("-----------------------------");
string[] v_fruits = { "Apple", "Orange", "Banana", "Strawberry", "Peach" };
string[] v_prices = { "10$", "20$", "15$", "50$", "100$" };
for (int i = 0; i < 5; i++)
{
if (i < 4) MyPrint.WriteLine($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}]");
else MyPrint.WriteLine($"[Fruit:{v_fruits[i]},Price:{v_prices[i]}]");
}
MyPrint.WriteLine("-----------------------------");
如果该文章对你有帮助,请给作者点个赞吧!