当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > [新]可精确到1毫秒的用以在记录文件中写入自定义的调试信息(主要是时间)的组件

ASP.NET
使用NUnit进行单元测试
[FxCop.设计规则]1. 抽象类不应该拥有构造函数
遍历XML文档返回二维数组(ASP)(更新版)
c#中的interface abstract与virtual
[VB.NET] Single & Double
asp.net验证码生成类(参考)
JAVA开发者应该去的20个英文网站 [摘]
C#中关于GDI+输出的问题
XML 命名空间提供了一种避免元素命名冲突的方法。
VS.NET 2005 Beta 2初体验(3)—操作SQL Mobile数据库
VS.NET 2005 Beta 2初体验(1)-用C#开发Managed代码
VS.NET 2005 Beta 2初体验(2)-用C++开发Native代码
VS.NET 2005 Beta2初体验(4)-Notification控件
XmlHttp异步获取网站数据的例子
利用C#编写一个简单的抓网页应用程序
C#中结构与类的区别
在.NET中实现彩色光标,动画光标和自定义光标
C#2.0新特性探究之模拟泛型和内置算法
C#2.0 新特性探究之委托与匿名委托
获取本机的本地上网IP地址

ASP.NET 中的 [新]可精确到1毫秒的用以在记录文件中写入自定义的调试信息(主要是时间)的组件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 30 ::
收藏到网摘: n/a

上次我发布了一个用以在记录文件中写入自定义的调试信息(主要是时间)的组件,但由于CLR的限制,它只能精确到10毫秒左右。后来我参考了网络上的一篇文章:http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx(在这里首先向原作者表示感谢)通过调用系统API得到了可精确到1毫秒左右的时间记录。故特重新用C#重写了这个组件,与大家共享。//====================================================================//TraceSpy - 用以在记录文件中写入自定义的调试信息(开发者:林健)//====================================================================////属性:// TraceFileName - 记录文件名////方法:// ★文本写入方面// WriteText - 写入自定义文本// ClearAllText - 清除所有文本// ★时间记录方面// SetTimePoint - 设置时间起点// GetTimeSpanFromInit - 询问时间跨度(距离时间起点)// GetTimeSpanFromPrev - 询问时间跨度(距离上次询问时间)// ★自定义计数器// SetCounter - 设置自定义计数器// AddCounter - 累加自定义计数器////====================================================================using System;namespace TraceSpy{ public class TheTrace { //记录文件名 static public string TraceFileName = "Trace.txt"; //时间起点(初始为当前时刻) static private long InitTimePoint = TimeCounter.GetExactNow().Ticks; //上次询问时间点(初始为当前时刻) static private long PrevTimePoint = TimeCounter.GetExactNow().Ticks; //自定义计数器 static private int counter = 0; //写入自定义文本 static public void WriteText(string str) { WriteText(str, false); } static public void WriteText(string str, bool showTime) { FileWriter.WriteText(str, showTime); } //清除所有文本 static public void ClearAllText() { FileWriter.ClearAllText(); } //设置时间起点 static public void SetTimePoint() { SetTimePoint(""); } static public void SetTimePoint(string note) { InitTimePoint = TimeCounter.GetExactNow().Ticks; PrevTimePoint = TimeCounter.GetExactNow().Ticks; FileWriter.WriteText("设置时间起点[" + note + "]。", false); } //询问时间跨度(距离时间起点) static public decimal GetTimeSpanFromInit() { return GetTimeSpanFromInit(""); } static public decimal GetTimeSpanFromInit(string note) { PrevTimePoint = TimeCounter.GetExactNow().Ticks; decimal span; span = (decimal)(PrevTimePoint - InitTimePoint) / (decimal)10000; FileWriter.WriteText("询问时间跨度[" + note + "],距离时间起点为" + span.ToString() + "毫秒。", false); return span; } //询问时间跨度(距离上次询问时间) static public decimal GetTimeSpanFromPrev() { return GetTimeSpanFromPrev(""); } static public decimal GetTimeSpanFromPrev(string note) { long RectTimePoint =TimeCounter.GetExactNow().Ticks; decimal span; span = (decimal)(RectTimePoint - PrevTimePoint) / (decimal)10000; PrevTimePoint = RectTimePoint; FileWriter.WriteText("询问时间跨度[" + note + "],距离上次询问时间为" + span.ToString() + "毫秒。", false); return span; } //设置自定义计数器 static public int SetCounter() { return SetCounter(0); } static public int SetCounter(int num) { counter = num; FileWriter.WriteText("自定义计数器值设置为" + counter + "。", false); return counter; } //累加自定义计数器 static public int AddCounter() { return AddCounter(1); } static public int AddCounter(int num) { counter += num; FileWriter.WriteText("自定义计数器值累加到" + counter + "。", false); return counter; } }}using System;using System.Runtime.InteropServices;namespace TraceSpy{ internal class TimeCounter { [DllImport("kernel32.dll")] static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount); [DllImport("kernel32.dll")] static extern bool QueryPerformanceFrequency([In, Out] ref long lpFrequency); static long _f = 0; static private long GetTickCount() { long f = _f; if (f == 0) { if (QueryPerformanceFrequency(ref f)) { _f = f; } else { _f = -1; } } if (f == -1) { return Environment.TickCount * 10000; } long c = 0; QueryPerformanceCounter(ref c); return (long)(((double)c) * 1000 * 10000 / ((double)f)); } static long _tc = 0; static internal DateTime GetExactNow() { if (_tc == 0) { long tc = Ge