Skip to content

Commit 2ea829e

Browse files
committed
Logging updates
- Tried to use default date/time formats when possible (not sure it actually affects anything though) - Bolstered initialization routine for logging to provide better info and past events sitting in the buffer. - Broke out log line formatting into its own function - Return the data directory name back to what it was before.
1 parent dbe0c61 commit 2ea829e

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

WinNUT_V2/WinNUT-Client_Common/Logger.vb

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,38 @@
77
'
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

10+
Imports System.Globalization
1011
Imports System.IO
1112

1213
Public Class Logger
1314
#Region "Constants/Shared"
14-
Private Shared ReadOnly BASE_FILE_NAME = ProgramName ' "WinNUT-CLient"
1515
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"
16+
17+
#If DEBUG Then
18+
Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.InvariantInfo
19+
#Else
20+
Private Shared ReadOnly DEFAULT_DATETIMEFORMAT = DateTimeFormatInfo.CurrentInfo
21+
#End If
22+
23+
1824
' The subfolder that will contain logs.
19-
Public Const LOG_SUBFOLDER = "\Logs\"
25+
Public Const LOG_SUBFOLDER = "Logs"
26+
27+
Private Shared ReadOnly BASE_FILE_NAME = ProgramName
28+
Private ReadOnly TEventCache As New TraceEventCache()
2029
#End Region
2130

31+
#Region "Private/backing values"
32+
2233
Private LogFile As Logging.FileLogTraceListener
23-
Private ReadOnly TEventCache As New TraceEventCache()
24-
Public LogLevelValue As LogLvl
2534
Private L_CurrentLogData As String
2635
Private LastEventsList As New List(Of Object)
36+
Private _DateTimeFormatInfo As DateTimeFormatInfo = DEFAULT_DATETIMEFORMAT
37+
38+
#End Region
39+
40+
Public LogLevelValue As LogLvl
41+
2742
Public Event NewData(sender As Object)
2843

2944
#Region "Properties"
@@ -77,6 +92,16 @@ Public Class Logger
7792
End Get
7893
End Property
7994

95+
Public Property DateTimeFormatInfo As DateTimeFormatInfo
96+
Get
97+
Return _DateTimeFormatInfo
98+
End Get
99+
Set(value As DateTimeFormatInfo)
100+
_DateTimeFormatInfo = value
101+
End Set
102+
End Property
103+
104+
80105
#End Region
81106

82107
Public Sub New(LogLevel As LogLvl)
@@ -89,11 +114,22 @@ Public Class Logger
89114
.Append = True,
90115
.AutoFlush = True,
91116
.LogFileCreationSchedule = LOG_FILE_CREATION_SCHEDULE,
92-
.CustomLocation = baseDataFolder & LOG_SUBFOLDER,
117+
.CustomLocation = Path.Combine(baseDataFolder, LOG_SUBFOLDER),
93118
.Location = Logging.LogFileLocation.Custom
94119
}
95120

96-
LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me)
121+
LogTracing(String.Format("{0} {1} Log file init", LongProgramName, ProgramVersion), LogLvl.LOG_NOTICE, Me)
122+
123+
If LastEventsList.Count > 0 Then
124+
' Fill new file with the LastEventsList buffer
125+
LogFile.WriteLine("==== History of " & LastEventsList.Count & " previous events ====")
126+
127+
For index As Integer = 0 To LastEventsList.Count - 1
128+
LogFile.WriteLine(String.Format("[{0}] {1}", index + 1, LastEventsList(index)))
129+
Next
130+
End If
131+
132+
LogFile.WriteLine(String.Empty)
97133
End Sub
98134

99135
''' <summary>
@@ -132,17 +168,7 @@ Public Class Logger
132168
''' <param name="sender">What generated this message.</param>
133169
''' <param name="LogToDisplay">A user-friendly, translated string to be shown.</param>
134170
Public Sub LogTracing(message As String, LvlError As LogLvl, sender As Object, Optional LogToDisplay As String = Nothing)
135-
Dim Pid = TEventCache.ProcessId
136-
Dim SenderName
137-
' Handle a null sender
138-
If sender Is Nothing Then
139-
SenderName = "Nothing"
140-
Else
141-
SenderName = sender.GetType.Name
142-
End If
143-
144-
Dim EventTime = Now.ToLocalTime
145-
Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message
171+
Dim FinalMsg = FormatLogLine(message, LvlError, sender)
146172

147173
' Always write log messages to the attached debug messages window.
148174
#If DEBUG Then
@@ -166,4 +192,15 @@ Public Class Logger
166192
RaiseEvent NewData(sender)
167193
End If
168194
End Sub
195+
196+
Private Function FormatLogLine(message As String, logLvl As LogLvl, Optional sender As Object = Nothing)
197+
Dim Pid = TEventCache.ProcessId
198+
Dim SenderName = "Nothing"
199+
200+
If sender IsNot Nothing Then
201+
SenderName = sender.GetType.Name
202+
End If
203+
204+
Return String.Format("{0} [{1}, {2}]: {3}", Date.Now.ToString(_DateTimeFormatInfo), Pid, SenderName, message)
205+
End Function
169206
End Class

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Public Module WinNUT_Globals
2424
#End If
2525

2626
Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath()
27+
Private ReadOnly DATA_DIRECTORY_NAME = "WinNut-Client"
2728

2829
Public ReadOnly ProgramName = My.Application.Info.ProductName
2930
Public ReadOnly LongProgramName = My.Application.Info.Description

0 commit comments

Comments
 (0)