'====================================================================
'TraceSpy - 用以在记录文件中写入自定义的调试信息(开发者:林健)
'====================================================================
'
'属性:
' TraceFileName - 记录文件名
'
'方法:
' ★文本写入方面
' WriteText - 写入自定义文本
' ClearAllText - 清除所有文本
' ★时间记录方面
' SetTimePoint - 设置时间起点
' GetTimeSpanFromInit - 询问时间跨度(距离时间起点)
' GetTimeSpanFromPrev - 询问时间跨度(距离上次询问时间)
'
'====================================================================
Public Class TraceSpy
'记录文件名
Public Shared TraceFileName As String = "Trace.txt"
'时间起点(初始为当前时刻)
Private Shared InitTimePoint As Long = Now.Ticks
'上次询问时间点(初始为当前时刻)
Private Shared PrevTimePoint As Long = Now.Ticks
'写入自定义文本
Public Shared Sub WriteText(ByVal str As String, Optional ByVal showTime As Boolean = False)
TraceSpyFileWriter.WriteText(str, showTime)
End Sub
'清除所有文本
Public Shared Sub ClearAllText()
TraceSpyFileWriter.ClearAllText()
End Sub
'设置时间起点
Public Shared Sub SetTimePoint(Optional ByVal note As String = "")
InitTimePoint = Now.Ticks
PrevTimePoint = Now.Ticks
TraceSpyFileWriter.WriteText("设置时间起点[" & note & "]。")
End Sub
'询问时间跨度(距离时间起点)
Public Shared Function GetTimeSpanFromInit(Optional ByVal note As String = "") As Decimal
PrevTimePoint = Now.Ticks
Dim span As Decimal
span = CDec(PrevTimePoint - InitTimePoint) / 10000D
TraceSpyFileWriter.WriteText("询问时间跨度[" & note & "],距离时间起点为" & span.ToString() & "毫秒。")
Return span
End Function
'询问时间跨度(距离上次询问时间)
Public Shared Function GetTimeSpanFromPrev(Optional ByVal note As String = "") As Decimal
Dim RectTimePoint As Long = Now.Ticks
Dim span As Decimal
span = CDec(RectTimePoint - PrevTimePoint) / 10000D
PrevTimePoint = RectTimePoint
TraceSpyFileWriter.WriteText("询问时间跨度[" & note & "],距离上次询问时间为" & span.ToString() & "毫秒。")
Return span
End Function
End Class
Friend Class TraceSpyFileWriter
Private Shared FileWriter As System.IO.StreamWriter
'向文件中写入一个字串
Friend Shared Sub WriteText(ByVal str As String, Optional ByVal showTime As Boolean = False)
If TraceSpy.TraceFileName = String.Empty Then
Exit Sub
End If
FileWriter = New System.IO.StreamWriter(TraceSpy.TraceFileName, True, Text.Encoding.Default)
Dim words As String
words = str
If showTime Then
words &= " @ " & Now.ToLongDateString & " " & Now.ToLongTimeString
End If
FileWriter.WriteLine(words)
FileWriter.Close()
End Sub
'清除记录文件
Friend Shared Sub ClearAllText()
If TraceSpy.TraceFileName = String.Empty Then
Exit Sub
End If
FileWriter = New System.IO.StreamWriter(TraceSpy.TraceFileName, False, Text.Encoding.Default)
FileWriter.Write("")
FileWriter.Close()
End Sub
End Class
本文地址:http://com.8s8s.com/it/it41120.htm