C#基于内置函数的自定义控制台输出函数

(一)设计目的

       对于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.");
图 1 测试结果

 

②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);
}
图 2 测试结果

 

③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.");
图 3 测试结果

 

(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.");
图 4 测试结果

 

②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);
}
图 5 测试结果

 

③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.");
图 6 测试结果

 

(四)测试总结

        根据上述测试结果可见,在忽略输出结果中的标记"(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("-----------------------------");
图7 有标记且根据筛选器筛选标记信息

 

图8 有标记但没使用筛选器

        图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输出。

图9 有标记且根据筛选器筛选标记信息

 

图10 有标记但没使用筛选器

        通过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("-----------------------------");
图11 有标记且根据筛选器筛选标记信息
图12 有标记但没使用筛选器

 

如果该文章对你有帮助,请给作者点个赞吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值