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]






本文转自LoveJenny博客园博客,原文链接:http://www.cnblogs.com/LoveJenny/p/LoggingWithDebugAndTrace.html,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
Java 程序员
【日志级别】log4j的8个日志级别(OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、ALL)
【日志级别】log4j的8个日志级别(OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、ALL)
374 0
|
8月前
|
存储 C++
神秘的 _DEBUG 宏从何处来?
神秘的 _DEBUG 宏从何处来?
|
10月前
|
Python
logging
logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级、日志保存路径、日志输出格式等。
|
网络协议
|
网络协议
|
Linux Apache
|
网络安全 Ruby