A component that can be accurate to 1 milliseconds to write custom debug information (primarily time) in a record file

Source: Internet
Author: User
Tags bool current time datetime empty set time tostring
Last time I published a component that writes custom debug information (mainly time) in a record file, but because of the limitations of the CLR, it can only be accurate to about 10 milliseconds. I later referred to an article on the Web: http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx (Thanks to the original author here first) gets a time record that can be accurate to about 1 milliseconds by invoking the system API. So I rewrote this component in C # to share with you.

//====================================================================
Tracespy-To write custom debug information in a record file (developer: Lin Jian)
//====================================================================
//
Property:
TraceFileName-Record file name
//
Method:
★ Text Writing Aspect
WRITETEXT-Write custom text
Clearalltext-Clear All text
★ Time Recording Aspect
Settimepoint-Set Time start
Gettimespanfrominit-Inquiry time span (distance time start)
Gettimespanfromprev-Query time span (distance from last inquiry)
★ Custom Counters
Setcounter-Set up custom counters
AddCounter-Add Custom counter
//
//====================================================================

Using System;

Namespace Tracespy
{
public class Thetrace
{

Record file name
static public string tracefilename = "Trace.txt";

Time start (initial for current time)
static private Long Inittimepoint = Timecounter.getexactnow (). Ticks;

Last query point (initial current time)
static private Long Prevtimepoint = Timecounter.getexactnow (). Ticks;

Custom Counters
static private int counter = 0;

Writing custom text
static public void WriteText (String str)
{
WRITETEXT (str, FALSE);
}

static public void WriteText (String str, bool showTime)
{
Filewriter.writetext (str, showTime);
}

Clear All text
static public void Clearalltext ()
{
Filewriter.clearalltext ();
}

Set Time start
static public void Settimepoint ()
{
Settimepoint ("");
}

static public void Settimepoint (string note)
{
Inittimepoint = Timecounter.getexactnow (). Ticks;
Prevtimepoint = Timecounter.getexactnow (). Ticks;
Filewriter.writetext ("Set time start [+ Note +]]." ", false);
}

Ask time span (distance time start)
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 ("Ask the time span [" + Note + "], distance from the beginning of time" + span. ToString () + "Ms. ", false);
return span;
}

Ask time span (distance from last inquiry)
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 ("Ask the time span [+ Note +]"], distance from the last Question Time "+ span. ToString () + "Ms. ", false);
return span;
}

Set up custom Counters
static public int Setcounter ()
{
Return Setcounter (0);
}
static public int setcounter (int num)
{
counter = num;
Filewriter.writetext (The custom counter value is set to + Counter +). ", false);
return counter;
}

Add Custom Counter
static public int AddCounter ()
{
return AddCounter (1);
}
static public int addcounter (int num)
{
Counter + = num;
Filewriter.writetext ("Add custom counter values to + 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 = GetTickCount ();
DateTime dt = DateTime.Now;
_TC = dt. TICKS-TC;
return DT;
}
return new DateTime (_TC + GetTickCount ());
}
}
}

Using System;

Namespace Tracespy
{
Internal class FileWriter
{
static private System.IO.StreamWriter writer;

Write a string to a file
static internal void WriteText (string str, bool showTime)
{
if (Thetrace.tracefilename = = String.Empty)
Return
writer = new System.IO.StreamWriter (Thetrace.tracefilename, True, System.Text.Encoding.Default);
string words;
words = str;
if (showTime)
Words + = "@" + timecounter.getexactnow (). Tolongdatestring () + "" + Timecounter.getexactnow (). Tolongtimestring ();
Writer. WriteLine (words);
Writer. Close ();
}

Purge record files
static internal void Clearalltext ()
{
if (Thetrace.tracefilename = = String.Empty)
Return
writer = new System.IO.StreamWriter (Thetrace.tracefilename, False, System.Text.Encoding.Default);
Writer. Write ("");
Writer. Close ();
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.