Logging with Debug And Trace (一)
对于一个应用程序而言,Log 必不可少.
在.net 里面,最简单的方式就是用Console 来输出 信息了,例如下面的例子:
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));
}
}
}

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

为了能够更方便的看清楚记录的Log 内容,有一种简单的方式: 把Console 替换成 Debug。
public class Program
{ public static void Main(string[] args)
{
One();
Two();
RecursiveTest(0, 500);
Console.ReadLine();
}
private static void One()
{
Debug.WriteLine("One");
}
private static int Two()
{
Debug.WriteLine("Return 2");
return 2;
}
private static void RecursiveTest(int from,int to)
{
if (from < to)
{
Debug.WriteLine(string.Format("{0} Enter Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
RecursiveTest(from + 1, to);
Debug.WriteLine(string.Format("{0} Exit Recursive Test:{1}", string.Join("", Enumerable.Repeat(" ", from)), from));
}
}
}
然后就可以在Output窗口下看到内容了。

Debug 的代码在release 模式下并不会真正的执行,这得益于条件编译,如果要在release 模式下也记录日志的话,那么可以使用Trace。
Trace 还提供了多种方法,可以记录Information,Error 等。
可能你已经注意到了把 Console 切换到Debug 后,控制台反而没有输出消息了。

这是因为Debug 的默认输出是Output 窗口,如果想要在控制台上显示的话,应该把 控制台添加到Debug 的输出中去,例如:
public static void Main(string[] args)
{
Debug.Listeners.Add(new ConsoleTraceListener(true));
One();
Two();
RecursiveTest(0, 5);
Console.ReadLine();
}

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


例如,我想要把日志输出到EventLog 里面去:
Debug.Listeners.Add(new EventLogTraceListener("DebugAndTrace"));

除了在代码中Hard code 代码之外,还可以使用Config 文件。
<?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>
输出如下:

Logging with Debug And Trace (一)的更多相关文章
- java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close(). java.lang.Exception: DEBUG ...
- 8个日志级别(OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL)
log4j定义了8个级别的log(除去OFF和ALL,可以说分为6个级别),优先级从高到低依次为:OFF.FATAL.ERROR.WARN.INFO.DEBUG.TRACE. ALL. ALL 最低等 ...
- C# Debug和Trace:输出调试信息
在 C# 语言中允许在程序运行时输出程序的调试信息,类似于使用 Console.WriteLine 的方式向控制台输出信息.所谓调试信息是程序员在程序运行时需要获取的程序运行的过程,以便程序员更好地解 ...
- Debug与Trace工具类的应用
在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...
- DEBUG STACK TRACE for PoolBackedDataSource.close().
我使用generator生成的代码,xml里面的内容没有覆盖重写,而是在下面直接追加,所以需要把以前的全部删除,然后在用generator生成.如果这个不能解决问题,就查查别人的问题.
- debug,trace,release项目配置区别
Debug模式是用来调试用的,它生成的执行文件中含有大量调试信息,所以很大: Release模式生成的执行文件消除了这些调试信息,可用来作为成品发布 Debug只在debug状态下会输出,Trace在 ...
- Trace、Debug和TraceSource的使用以及日志设计 .
[-] Trace 和 Debug区别 什么是Listeners 跟踪开关 使用BooleanSwitch开关 使用TraceSwitch开关 使用TraceSource代替Trace和Debug 设 ...
- C#学习笔记14——TRACE、DEBUG和TRACESOURCE的使用以及日志设计
Trace.Debug和TraceSource的使用以及日志设计 .NET Framework 命名空间 System.Diagnostics 包含用于跟踪执行流程的 Trace.Debug 和 ...
- C# 调试之 Debug.WriteLine()、Trace.WriteLine()
Trace 类 和 Debug 类的区别在于,Trace 类会同时在 Debug.Release 模式下起作用,而 Debug 只作用在 Debug 模式下. 区别: 1. 输出跟踪信息 Trace. ...
随机推荐
- jQuery中width、innerWidth、outerWidth的区别
原文:摘自http://www.canaansky.com/blog/107/ 在css的盒子模型中,最内部是content area,然后是padding.border.margin 那么width ...
- 一篇笔记整理JVM工作原理
首先要了解的 >>数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型. 基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了 ...
- Problem with "AnyConnect was not able to establish connection to the specified secure gateway."
Cisco的VPN客户端最近报"AnyConnect was not able to establish connection to the specified secure gateway ...
- mysql中ip和整数的转换
INET_ATON(expr) 给出一个作为字符串的网络地址的点地址表示,返回一个代表该地址数值的整数.地址可以是4或8比特地址. mysql> SELECT INET_ATON('209.20 ...
- string与int互换
1:将string转化为int 1.) int i = Integer.parseInt(String s); 2.) int i = Integer.valueOf(my_str).intValue ...
- retrofit2的get和post
get: 例: @GET("room/question_focus") Call<BaseResponseEntity> followQuestion(@Query(& ...
- Android获取ImageView上的图片,和一个有可能遇到的问题!
1.在获取图片前先调用setDrawingCacheEnabled(true)这个方法: 举例:mImageView.setDrawingCacheEnabled(true); 2.之后可以通过get ...
- Python学习日志(二)
在网易云课堂看到小甲鱼的python视频,想起以前看就是看他的视频学C的虽然后来不了了之都怪我自己啦,于是决定跟着这个视频来学python啦! IDLE IDLE其实是一个python shell , ...
- Git命令参考手册(文本版)
git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...
- Java随机生成18位身份证号
package com.ihome.data; import java.text.SimpleDateFormat; import java.util.Calendar; import java.ut ...