EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

Logging with Debug And Trace (一)

1
对于一个应用程序而言,Log 必不可少.

在.net 里面,最简单的方式就是用Console 来输出 信息了,例如下面的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Program
{
    public static void Main(string[] args)
    {
        One();
        Two();
        RecursiveTest(0, 5);
        Console.ReadLine();
    }
    private static void One()
    {
        Console.WriteLine("One");
    }
    private static int Two()
    {
        Console.WriteLine("Return 2");
        return 2;
    }
    private static void RecursiveTest(int from, int to)
    {
        if (from < to)
        {
            Console.WriteLine(string.Format("{0} Enter Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
            RecursiveTest(from + 1, to);
            Console.WriteLine(string.Format("{0} Exit  Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
        }
    }
}

clipboard

这种方式的有点是简单,快捷,很容易的就可以输出你所感兴趣的内容,可是缺点也很明显,就是当内容多的时候,看不清:

clipboard[1]

为了能够更方便的看清楚记录的Log 内容,有一种简单的方式: 把Console 替换成 Debug。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Program
{
 
  public static void Main(string[] args)
  {
   One();
   Two();
   RecursiveTest(0, 500);
   Console.ReadLine();
  }
  private static void One()
  {
   <strong>Debug</strong>.WriteLine("One");
  }
  private static int Two()
  {
   <strong>Debug</strong>.WriteLine("Return 2");
   return 2;
  }
  private static void RecursiveTest(int from,int to)
  {
   if (from < to)
   {
    <strong>Debug</strong>.WriteLine(string.Format("{0} Enter Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
    RecursiveTest(from + 1, to);
    <strong>Debug</strong>.WriteLine(string.Format("{0} Exit  Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
   }
  }
}

然后就可以在Output窗口下看到内容了。

clipboard[2]

Debug 的代码在release 模式下并不会真正的执行,这得益于条件编译,如果要在release 模式下也记录日志的话,那么可以使用Trace。

Trace 还提供了多种方法,可以记录Information,Error 等。

可能你已经注意到了把 Console 切换到Debug 后,控制台反而没有输出消息了。

clipboard[3]

这是因为Debug 的默认输出是Output 窗口,如果想要在控制台上显示的话,应该把 控制台添加到Debug 的输出中去,例如:

1
2
3
4
5
6
7
8
public static void Main(string[] args)
 {
  Debug.Listeners.Add(new ConsoleTraceListener(true));
  One();
  Two();
  RecursiveTest(0, 5);
  Console.ReadLine();
 }

clipboard[4]

事实上,你除了添加ConsoleTraceListener,你还可以添加下面几种Listener。

clipboard[5]

clipboard[6]

例如,我想要把日志输出到EventLog 里面去:

Debug.Listeners.Add(new EventLogTraceListener("DebugAndTrace"));

clipboard[7]

除了在代码中Hard code 代码之外,还可以使用Config 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8" ?>
 
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.diagnostics>
    <trace autoflush="true" indentsize="4">
 
     <listeners>
      <add name="console" type="System.Diagnostics.ConsoleTraceListener" />
      <add name="fileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
     </listeners>
    </trace>
  </system.diagnostics>
</configuration>

输出如下:

clipboard[8]

 

posted @   LoveJenny  阅读(1631)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
EasyText, EasyLicense 的作者, https://github.com/EasyHelper Good Good Study,Day Day Up.

 

点击右上角即可分享
微信分享提示