using System.Diagnostics 命名空间 包含了能够与系统进程 事件日志 和性能计数器进行交互的类 一般用于帮助诊断和调试应用程序 例如 Debug类用于帮组调试代码 Process类能够控制进程访问 Trace类能够跟踪代码的执行情况
Process 用于操作本地或者远程进程打访问 通过Process 可以在托管环境下很容易的操作对外部进程的启动或者停止,必须设置相应的FileName和Arguments属性 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "iexplore.exe";
myProcess.StartInfo.Arguments = "
http://www.baidu.com";
myProcess.Start();
}
}
}
Stopwatch 用于高精度检测运行时间 Start方法表示开始测量 Stop表示停止测量 Reset 表示停止测量并重置 为0最后以Elapsed返回测量时间:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
ArrayList mylist = new ArrayList();
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
mylist.Add(i);
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
Console.ReadLine();
}
}
}
EventLog 提供了写入 读取 创建 和删除事件日志的方法 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
EventLog mylog = new EventLog("MyLog", ".", "AnyLog");
mylog.WriteEntry("It is my log.", EventLogEntryType.Information, 1);
foreach (EventLogEntry ele in mylog.Entries)
{
Console.WriteLine(ele.Message);
}
}
}
}
debug类:
调试程序对每个程序员来说是家常便饭。
可是我们会经常遇到一些情况让我们头疼,例如:
当我们在开发一个界面控件的时候,简单的设断点会增加paint事件的响应次数,而造成的环境参数改变。 断点设多了,程序常常停在正常运行的地方;这样一来,调试一个错误要花费大量时间去寻找错误。
这时,我们就需要利用system.diagnostics.debug类来帮助我们调试。我们可以通过调用debug.writeline(string
message)函数,将我们所关心的信息打印在visual studio
ide的output窗口中。也可以利用debug.assert(bool condition)来让程序停在错误的地方,并且显示call
stack。
debug类中所有函数的调用都不会在release版本里有效。也就是说,我们通过这种方法所加的代码可以仅用于调试;在发布的时候无需删任何代码,就可以给用户一个没有调试指令的程序了。