1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| Public Enum ProfilTrace
Defaut
'None
'All
StartChrono
StopChrono
End Enum
Public Class clsTraceSource
Inherits TraceSource
Private _twfileSize As Integer = 100000 ' Taille des fichiers TextWriterTraceListener par défaut = 100ko
Dim _Chronos As New System.Collections.Generic.Dictionary(Of String, Stopwatch)
Dim _IDChronos As New System.Collections.Generic.Dictionary(Of String, Integer)
Dim _Compteur As Integer = 0
'Dim _TraceOptionalOutPutOptions As Boolean
'Dim Stopwatch As System.Diagnostics.Stopwatch
Public Sub New(ByVal name As String)
MyBase.New(name)
End Sub
Public Property TextWriterFileSize() As Integer
Get
Return _twfileSize
End Get
Set(ByVal value As Integer)
_twfileSize = value
End Set
End Property
'Public Property TraceOptionalOutPutOptions() As Boolean
' Get
' Return _TraceOptionalOutPutOptions
' End Get
' Set(ByVal value As Boolean)
' _TraceOptionalOutPutOptions = value
' End Set
'End Property
Public Sub WriteAppInfos()
Dim strProductName = My.Application.Info.ProductName
Dim strVersion = String.Format("Version {0}", My.Application.Info.Version.ToString)
Dim strCopyright = My.Application.Info.Copyright
Dim strCompanyName = My.Application.Info.CompanyName
Dim strDescription = My.Application.Info.Description
Dim strToday As String = "Date : " & DateTime.Now.ToShortDateString
Dim strCompilMode As String = "Mode Compilation=Release"
#If Debug = True Then 'Code executé uniquement en mode Debug
strCompilMode = "Mode Compilation=Debug"
#End If
Dim message As String
message = strProductName & " - " & strVersion & Environment.NewLine & strCompilMode & Environment.NewLine & strToday
For Each _listener As TraceListener In Me.Listeners
_listener.WriteLine(message)
_listener.Flush()
Next
End Sub
Public Sub WriteTraceInfos()
Dim message As String = String.Format("---Le Logger {0} est démarré avec TraceSwitchLevel = {1}---", Me.Name, Me.Switch.Level.ToString)
For Each _listener As TraceListener In Me.Listeners
_listener.WriteLine(message)
_listener.Flush()
Next
End Sub
Public Overloads Sub TraceEvent(eventType As System.Diagnostics.TraceEventType, id As Integer, message As String)
Call _TraceEvent(eventType, id, message, ProfilTrace.Defaut)
End Sub
Public Overloads Sub TraceEvent(eventType As System.Diagnostics.TraceEventType, id As Integer, message As String, profil As ProfilTrace)
Call _TraceEvent(eventType, id, message, profil)
End Sub
Private Sub _TraceEvent(eventType As System.Diagnostics.TraceEventType, id As Integer, message As String, _
profil As ProfilTrace)
Dim val(11) As String ' = {"A", "B"}
Dim pref(11) As String ' = {"Date = ", "Time = "}
Dim arg(11) As String
Static i As Integer : i += 1
arg(6) = "[" & i.ToString & "]"
arg(7) = Convert.ToChar(Keys.Tab) & DateTime.Now.ToLongTimeString & " " & DateTime.Now.Millisecond
arg(9) = Convert.ToChar(Keys.Tab) & "-" & [Enum].GetName(GetType(TraceEventType), eventType) & "-"
If eventType = TraceEventType.Error Then
arg(9) = arg(9).ToUpper ' si TraceLevel = TraceError on affiche TraceLevel en Majuscule
message = arg(9) & ":" & message ' si TraceLevel = TraceError on rajoute ERROR: devant le libellé
End If
Dim strSubCurrent As String = ""
Dim strModuleParent As String = ""
Dim strSubModule As String = ""
Dim stTraceFrame As StackFrame = New StackFrame(2)
Dim methode As System.Reflection.MethodBase = stTraceFrame.GetMethod
strSubCurrent = methode.Name
strModuleParent = methode.DeclaringType.FullName
Dim SubFullName As String = strModuleParent & "/" & strSubCurrent
arg(10) = Convert.ToChar(Keys.Tab) & " [" & strModuleParent & "/" & Convert.ToChar(Keys.Tab) & strSubCurrent & "]"
message = Convert.ToChar(Keys.Tab) & " => " & message
If eventType <= Me.Switch.Level Then
For Each _listener As TraceListener In Me.Listeners
If _listener.GetType Is GetType(TextWriterTraceListener) Then
'Dim strFilePath = FilePath(_listener)
'Call ControlFileSize(strFilePath)
Call ControlFileSize2(CType(_listener, TextWriterTraceListener))
End If
For n = 0 To 5
val(n) = "" : pref(n) = "" : arg(n) = ""
Next
Dim op As TraceOptions = _listener.TraceOutputOptions
Dim eventcache As New TraceEventCache
'If a.HasFlag(TraceOptions.None) Then Beep()
If op.HasFlag(TraceOptions.LogicalOperationStack) Then pref(0) = " - LogicalOperationStack = " : val(0) = eventcache.LogicalOperationStack.ToString : arg(0) = Convert.ToChar(Keys.Tab) & pref(0) & val(0)
If op.HasFlag(TraceOptions.DateTime) Then pref(1) = " - DateTime = " : val(1) = CStr(eventcache.DateTime) : arg(1) = Convert.ToChar(Keys.Tab) & pref(1) & val(1)
If op.HasFlag(TraceOptions.Timestamp) Then pref(2) = " - Timestamp = " : val(2) = CStr(eventcache.Timestamp) : arg(2) = Convert.ToChar(Keys.Tab) & pref(2) & val(2)
If op.HasFlag(TraceOptions.ProcessId) Then pref(3) = " - ProcessId = " : val(3) = CStr(eventcache.ProcessId) : arg(3) = Convert.ToChar(Keys.Tab) & pref(3) & val(3)
If op.HasFlag(TraceOptions.ThreadId) Then pref(4) = " - ThreadId = " : val(4) = CStr(eventcache.ProcessId) : arg(4) = Convert.ToChar(Keys.Tab) & pref(4) & val(4)
If op.HasFlag(TraceOptions.Callstack) Then pref(5) = " - Callstack = " : val(5) = eventcache.Callstack : arg(5) = Convert.ToChar(Keys.Tab) & pref(5) & val(5)
'Dim strProfil As String = ""
'If showId = False Then arg(7) = ""
'If showTime = False Then arg(8) = ""
'If showEventType = False Then arg(9) = ""
'If showEventType = False Then arg(10) = ""
If eventType <= TraceEventType.Warning Or eventType >= TraceEventType.Verbose Then
arg(8) = Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab)
Else
arg(8) = Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) : arg(10) = ""
End If
Select Case profil
'Case ProfilTrace.None
' arg(6) = "" : arg(7) = "" : arg(8) = Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) : arg(9) = "" : arg(10) = ""
'Case ProfilTrace.Standard
' arg(8) = Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) : arg(10) = ""
'Case ProfilTrace.All
' arg(8) = Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab) & Convert.ToChar(Keys.Tab)
Case ProfilTrace.StartChrono
Call StartChrono(SubFullName)
arg(8) = Convert.ToChar(Keys.Tab) & "[Chrono" & GetIdChrono(SubFullName) & ":Start]"
Case ProfilTrace.StopChrono
arg(8) = Convert.ToChar(Keys.Tab) & "[Chrono" & GetIdChrono(SubFullName) & ":" & Math.Round(StopChrono(SubFullName), 2).ToString & "ms]"
End Select
Dim msg As String = ""
'If Me.TraceOptionalOutPutOptions = True Then
msg = arg(6) & arg(7) & arg(8) & Convert.ToChar(Keys.Tab) & arg(9) & arg(0) & arg(1) & arg(2) & arg(3) & arg(4) & arg(5) & arg(10) & message
'Else
'msg = arg(6) & arg(7) & arg(8) & Convert.ToChar(Keys.Tab) & arg(9) & arg(10) & message
'End If
_listener.WriteLine(msg)
'_listener.Flush()
Next
End If
End Sub
Private Sub ControlFileSize2(ByVal listener As TextWriterTraceListener)
Dim tw As TextWriterTraceListener = CType(listener, TextWriterTraceListener)
Dim sw As System.IO.StreamWriter = CType(tw.Writer, IO.StreamWriter)
Dim bs As System.IO.FileStream = CType(sw.BaseStream, IO.FileStream)
Dim _filepath = bs.Name
Try
Dim Fi = New System.IO.FileInfo(_filepath)
If Fi.Exists And Fi.Length > _twfileSize Then
Me.Close()
Fi.Delete()
End If
Fi = Nothing
Catch ex As Exception
End Try
End Sub
'Private Function FilePath(ByVal listener As TraceListener) As String
' 'Dim finfo As System.Reflection.FieldInfo = listener.GetType.GetField("initializeData", Reflection.BindingFlags.NonPublic + Reflection.BindingFlags.Instance)
' 'Dim filename As String = finfo.GetValue(listener)
' Dim tw As TextWriterTraceListener = CType(listener, TextWriterTraceListener)
' Dim sw As System.IO.StreamWriter = CType(tw.Writer, IO.StreamWriter)
' Dim bs As System.IO.FileStream = CType(sw.BaseStream, IO.FileStream)
' Dim _filepath = bs.Name
' Return _filepath
'End Function
'Private Sub ControlFileSize(ByVal filepath As String)
' Try
' Dim Fi = New System.IO.FileInfo(filepath)
' If Fi.Exists And Fi.Length > _twfileSize Then
' Me.Close()
' Fi.Delete()
' End If
' Fi = Nothing
' Catch ex As Exception
' End Try
'End Sub
Private Function GetIdChrono(ByVal name As String) As Integer
If _IDChronos.ContainsKey(name) Then
Return _IDChronos(name)
Else
Return -1
End If
End Function
Private Sub StartChrono(ByVal name As String)
If _Chronos.ContainsKey(name) Then
_Chronos(name).Reset()
Else
_Chronos.Add(name, New Stopwatch)
_IDChronos.Add(name, _Compteur)
_Compteur += 1
End If
_Chronos(name).Start()
End Sub
Private Function StopChrono(ByVal name As String) As Double
Dim Value As Double
If _Chronos.ContainsKey(name) Then
_Chronos(name).Stop()
Value = _Chronos(name).Elapsed.TotalMilliseconds
Else
'_Chronos.Add("tel nom", New Stopwatch)
End If
Return Value
End Function
'Private Sub Watching(ByVal Value As String)
' If Value = "Start" Then
' ' Initialisation : debut de la mesure
' Stopwatch.Start()
' ElseIf Value = "Stop" Then
' ' fin de la mesure
' Stopwatch.Stop()
' ' Affichage de la mesure
' Console.WriteLine(Stopwatch.Elapsed().TotalMilliseconds & " millisecondes")
' Console.ReadKey()
' 'MessageBox.Show(Stopwatch.Elapsed().TotalSeconds & " secondes")
' End If
'End Sub
End Class |
Partager