10
10
Imports System.IO
11
11
12
12
Public Class Logger
13
- Private Const BaseFileName = "WinNUT-CLient"
13
+ # Region "Constants/Shared"
14
+ Private Shared ReadOnly BASE_FILE_NAME = ProgramName ' "WinNUT-CLient"
15
+ Private Const LOG_FILE_CREATION_SCHEDULE = Logging.LogFileCreationScheduleOption.Daily
16
+ ' The LogFileCreationScheduleOption doesn't present the string format of what it uses
17
+ Private Const LOG_FILE_DATESTRING = "yyyy-MM-dd"
14
18
' Logs will be stored in the program's appdata folder, in a Logs subdirectory.
15
19
Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs" )
20
+ # End Region
16
21
17
22
Private LogFile As Logging.FileLogTraceListener
18
23
Private ReadOnly TEventCache As New TraceEventCache()
19
24
Public LogLevelValue As LogLvl
20
25
Private L_CurrentLogData As String
21
26
Private LastEventsList As New List( Of Object )
22
- Public Event NewData( ByVal sender As Object )
27
+ Public Event NewData(sender As Object )
23
28
24
29
# Region "Properties"
25
30
31
+ Private _MaxEvents As Integer = 200
32
+ Public Property MaxEvents As Integer
33
+ Get
34
+ Return _MaxEvents
35
+ End Get
36
+ Set (value As Integer )
37
+ If value < 0 Then
38
+ Throw New ArgumentOutOfRangeException( "MaxInteger" , "Maximum number of events cannot be negative." )
39
+ End If
40
+ End Set
41
+ End Property
42
+
26
43
Public Property CurrentLogData() As String
27
44
Get
28
- Dim Tmp_Data = Me . L_CurrentLogData
29
- Me . L_CurrentLogData = Nothing
45
+ Dim Tmp_Data = L_CurrentLogData
46
+ L_CurrentLogData = Nothing
30
47
Return Tmp_Data
31
48
End Get
32
- Set ( ByVal Value As String )
33
- Me . L_CurrentLogData = Value
49
+ Set (Value As String )
50
+ L_CurrentLogData = Value
34
51
End Set
35
52
End Property
53
+
36
54
Public ReadOnly Property LastEvents() As List( Of Object )
37
55
Get
38
- Return Me . LastEventsList
56
+ Return LastEventsList
39
57
End Get
40
58
End Property
41
59
42
60
''' <summary>
43
61
''' Returns if data is being written to a file. Also allows for file logging to be setup or stopped.
44
62
''' </summary>
45
63
''' <returns>True when the <see cref="LogFile"/> object is instantiated, false if not.</returns>
46
- Public Property IsWritingToFile() As Boolean
64
+ Public ReadOnly Property IsWritingToFile() As Boolean
47
65
Get
48
- Return Not ( LogFile Is Nothing )
66
+ Return LogFile IsNot Nothing
49
67
End Get
50
68
51
- Set (Value As Boolean )
52
- If Value = False And LogFile IsNot Nothing Then
53
- LogFile.Close()
54
- LogFile.Dispose()
55
- LogFile = Nothing
56
- LogTracing( "Logging to file has been disabled." , LogLvl.LOG_NOTICE, Me )
57
- ElseIf Value Then
58
- SetupLogfile()
59
- End If
60
- End Set
69
+ 'Get
70
+ ' Return Not (LogFile Is Nothing)
71
+ 'End Get
72
+
73
+ 'Set(Value As Boolean)
74
+ ' If Value = False And LogFile IsNot Nothing Then
75
+ ' LogFile.Close()
76
+ ' LogFile.Dispose()
77
+ ' LogFile = Nothing
78
+ ' LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me)
79
+ ' ElseIf Value Then
80
+ ' SetupLogfile()
81
+ ' End If
82
+ 'End Set
61
83
End Property
62
84
85
+ ''' <summary>
86
+ ''' Either retrieve the log file location from the <see cref="LogFile"/> object, or give an estimate of what it
87
+ ''' would be.
88
+ ''' </summary>
89
+ ''' <returns>The possible path to the log file. Note that this does not gaurantee it exists.</returns>
63
90
Public ReadOnly Property LogFileLocation() As String
64
91
Get
65
92
If IsWritingToFile Then
66
93
Return LogFile.FullLogFileName
67
94
Else
68
- Return String .Empty
95
+ Return Path.Combine(LogFolder, BASE_FILE_NAME & Date .Now.ToString(LOG_FILE_DATESTRING))
69
96
End If
70
97
End Get
71
98
End Property
72
99
73
- Public Property LogLevel() As LogLvl
74
- Get
75
- Return Me .LogLevelValue
76
- End Get
77
- Set ( ByVal Value As LogLvl)
78
- Me .LogLevelValue = Value
79
- End Set
80
- End Property
100
+ ' Log all events - this object will keep all logs and allow accessors to decide which ones they want.
101
+ 'Public Property LogLevel() As LogLvl
102
+ ' Get
103
+ ' Return Me.LogLevelValue
104
+ ' End Get
105
+ ' Set(ByVal Value As LogLvl)
106
+ ' Me.LogLevelValue = Value
107
+ ' End Set
108
+ 'End Property
81
109
82
110
# End Region
83
111
84
- Public Sub New (WriteLog As Boolean , LogLevel As LogLvl)
85
- IsWritingToFile = WriteLog
112
+ Public Sub New (writeLog As Boolean , LogLevel As LogLvl)
86
113
LogLevelValue = LogLevel
87
- LastEventsList.Capacity = 50
114
+ ' LastEventsList.Capacity = 50
115
+
116
+ ' IsWritingToFile = writeLog
117
+ If writeLog = True Then
118
+ InitializeLogFile()
119
+ End If
88
120
End Sub
89
121
90
- Public Sub SetupLogfile ()
91
- LogFile = New Logging.FileLogTraceListener(BaseFileName ) With {
122
+ Public Sub InitializeLogFile ()
123
+ LogFile = New Logging.FileLogTraceListener(BASE_FILE_NAME ) With {
92
124
.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId,
93
125
.Append = True ,
94
126
.AutoFlush = True ,
95
- .LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily ,
127
+ .LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE ,
96
128
.CustomLocation = LogFolder,
97
129
.Location = Logging.LogFileLocation.Custom
98
130
}
99
131
100
132
LogTracing( "Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me )
101
133
End Sub
102
134
135
+ ''' <summary>
136
+ ''' Disable logging and delete the current file.
137
+ ''' </summary>
138
+ ''' <returns>True if file was successfully deleted. False if an exception was encountered.</returns>
103
139
Public Function DeleteLogFile() As Boolean
140
+ Dim fileLocation = LogFile.FullLogFileName
141
+
142
+ ' Disable logging first.
143
+ If LogFile IsNot Nothing Then
144
+ LogFile.Close()
145
+ LogFile.Dispose()
146
+ ' For some reason, the object needs to be dereferenced to actually get it to close the handle.
147
+ LogFile = Nothing
148
+ LogTracing( "Logging to file has been disabled." , LogLvl.LOG_NOTICE, Me )
149
+ End If
150
+
104
151
Try
105
- Dim fileLocation = LogFile.FullLogFileName
106
- IsWritingToFile = False
152
+ ' IsWritingToFile = False
107
153
File.Delete(fileLocation)
108
154
Return True
109
155
Catch ex As Exception
156
+ LogTracing( "Error when deleteing log file: " & ex.ToString(), LogLvl.LOG_ERROR, Me )
110
157
Return False
111
158
End Try
112
159
End Function
@@ -117,39 +164,34 @@ Public Class Logger
117
164
''' <paramref name="LogToDisplay"/> is specified.
118
165
''' </summary>
119
166
''' <param name="message">The raw information that needs to be recorded.</param>
120
- ''' <param name="LvlError">How important the information is .</param>
121
- ''' <param name="sender"></param>
167
+ ''' <param name="LvlError">The severity of the message .</param>
168
+ ''' <param name="sender">What generated this message. </param>
122
169
''' <param name="LogToDisplay">A user-friendly, translated string to be shown.</param>
123
- Public Sub LogTracing( ByVal message As String , ByVal LvlError As Int16 , sender As Object , Optional ByVal LogToDisplay As String = Nothing )
170
+ Public Sub LogTracing(message As String , LvlError As LogLvl , sender As Object , Optional LogToDisplay As String = Nothing )
124
171
Dim Pid = TEventCache.ProcessId
125
172
Dim SenderName = sender.GetType.Name
126
173
Dim EventTime = Now.ToLocalTime
127
174
Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message
128
175
129
- 'Update LogFilePath to make sure it's still the correct path
130
- ' gbakeman 31/7/2022: Disabling since the LogFilePath should never change throughout the lifetime of this
131
- ' object, unless proper initialization has occured.
132
-
133
- ' WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName
134
-
135
176
' Always write log messages to the attached debug messages window.
136
177
# If DEBUG Then
137
178
Debug.WriteLine(FinalMsg)
138
179
# End If
139
180
140
181
'Create Event in EventList in case of crash for generate Report
141
- If Me . LastEventsList.Count = Me .LastEventsList.Capacity Then
142
- Me . LastEventsList.RemoveAt(0 )
182
+ If LastEventsList.Count >= MaxEvents Then
183
+ LastEventsList.RemoveAt( 0 )
143
184
End If
185
+ LastEventsList.Add(FinalMsg)
144
186
145
- Me .LastEventsList.Add(FinalMsg)
146
-
147
- If IsWritingToFile AndAlso Me .LogLevel >= LvlError Then
187
+ ' Send message to log file if enabled
188
+ If IsWritingToFile AndAlso LogLevelValue >= LvlError Then
148
189
LogFile.WriteLine(FinalMsg)
149
190
End If
191
+
150
192
'If LvlError = LogLvl.LOG_NOTICE Then
151
193
If LogToDisplay IsNot Nothing Then
152
- Me . L_CurrentLogData = LogToDisplay
194
+ L_CurrentLogData = LogToDisplay
153
195
RaiseEvent NewData(sender)
154
196
End If
155
197
End Sub
0 commit comments