Skip to content

Commit ce5be39

Browse files
committed
Logging-related improvements
- WinNUT_Globals: - Move AppData directory info to a Property, and return different values if debugging or not. - Logger object is now stored in globals, rathern than WinNUT.vb - Logger.vb: - BaseFileName is now a const, LogFolder is shared and stored in a subdirectory - LogFile writing is now a property that handles setup and shutdown of logging - Actual logging setup is moved to its own subrouting, so the class constructor has very little to do. - Updated/simplified/commented out unnecessary LogFile references. - Pref_Gui: Simplified log controls, created a subrouting for log control enablement. - WinNUT.vb: - Instantiate Logger at beginning of form load. - Relocated logtracing lines to appropriate locations. - Renamed local Logger object for events handling.
1 parent ed88a65 commit ce5be39

File tree

9 files changed

+189
-116
lines changed

9 files changed

+189
-116
lines changed

WinNUT_V2/WinNUT-Client_Common/Logger.vb

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

10+
Imports System.IO
11+
1012
Public Class Logger
11-
Private ReadOnly LogFile As New Microsoft.VisualBasic.Logging.FileLogTraceListener()
13+
Private Const BaseFileName = "WinNUT-CLient"
14+
' Logs will be stored in the program's appdata folder, in a Logs subdirectory.
15+
Public Shared ReadOnly LogFolder = Path.Combine(ApplicationData, "Logs")
16+
17+
Private LogFile As Logging.FileLogTraceListener
1218
Private ReadOnly TEventCache As New TraceEventCache()
13-
' Enable writing to a log file.
14-
Public WriteToFile As Boolean
1519
Public LogLevelValue As LogLvl
1620
Private L_CurrentLogData As String
1721
Private LastEventsList As New List(Of Object)
1822
Public Event NewData(ByVal sender As Object)
1923

24+
#Region "Properties"
25+
2026
Public Property CurrentLogData() As String
2127
Get
2228
Dim Tmp_Data = Me.L_CurrentLogData
@@ -32,32 +38,38 @@ Public Class Logger
3238
Return Me.LastEventsList
3339
End Get
3440
End Property
35-
Public Sub New(ByVal WriteLog As Boolean, ByVal LogLevel As LogLvl)
36-
Me.WriteToFile = WriteLog
37-
Me.LogLevelValue = LogLevel
38-
Me.LogFile.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId
39-
Me.LogFile.Append = True
40-
Me.LogFile.AutoFlush = True
41-
Me.LogFile.BaseFileName = "WinNUT-CLient"
42-
Me.LogFile.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily
43-
Me.LogFile.Location = Microsoft.VisualBasic.Logging.LogFileLocation.Custom
44-
Me.LogFile.CustomLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client"
45-
Me.LastEventsList.Capacity = 50
46-
' WinNUT_Globals.LogFilePath = Me.LogFile.FullLogFileName
47-
End Sub
4841

49-
Public Property WriteLog() As Boolean
42+
''' <summary>
43+
''' Returns if data is being written to a file. Also allows for file logging to be setup or stopped.
44+
''' </summary>
45+
''' <returns>True when the <see cref="LogFile"/> object is instantiated, false if not.</returns>
46+
Public Property IsWritingToFile() As Boolean
5047
Get
51-
Return Me.WriteToFile
48+
Return Not (LogFile Is Nothing)
5249
End Get
53-
Set(ByVal Value As Boolean)
54-
Me.WriteToFile = Value
55-
If Not Me.WriteToFile Then
50+
51+
Set(Value As Boolean)
52+
If Value = False And LogFile IsNot Nothing Then
53+
LogFile.Close()
5654
LogFile.Dispose()
55+
LogFile = Nothing
56+
LogTracing("Logging to file has been disabled.", LogLvl.LOG_NOTICE, Me)
57+
ElseIf Value Then
58+
SetupLogfile()
5759
End If
5860
End Set
5961
End Property
6062

63+
Public ReadOnly Property LogFileLocation() As String
64+
Get
65+
If IsWritingToFile Then
66+
Return LogFile.FullLogFileName
67+
Else
68+
Return String.Empty
69+
End If
70+
End Get
71+
End Property
72+
6173
Public Property LogLevel() As LogLvl
6274
Get
6375
Return Me.LogLevelValue
@@ -67,10 +79,42 @@ Public Class Logger
6779
End Set
6880
End Property
6981

82+
#End Region
83+
84+
Public Sub New(WriteLog As Boolean, LogLevel As LogLvl)
85+
IsWritingToFile = WriteLog
86+
LogLevelValue = LogLevel
87+
LastEventsList.Capacity = 50
88+
End Sub
89+
90+
Public Sub SetupLogfile()
91+
LogFile = New Logging.FileLogTraceListener(BaseFileName) With {
92+
.TraceOutputOptions = TraceOptions.DateTime Or TraceOptions.ProcessId,
93+
.Append = True,
94+
.AutoFlush = True,
95+
.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily,
96+
.CustomLocation = LogFolder,
97+
.Location = Logging.LogFileLocation.Custom
98+
}
99+
100+
LogTracing("Log file is initialized at " & LogFile.FullLogFileName, LogLvl.LOG_NOTICE, Me)
101+
End Sub
102+
103+
Public Function DeleteLogFile() As Boolean
104+
Try
105+
Dim fileLocation = LogFile.FullLogFileName
106+
IsWritingToFile = False
107+
File.Delete(fileLocation)
108+
Return True
109+
Catch ex As Exception
110+
Return False
111+
End Try
112+
End Function
113+
70114
''' <summary>
71-
''' Insert an event into the <see cref="LastEventsList" /> for report generating, write a line to the
72-
''' <see cref="LogFile"/> if the event is as or more important than the <see cref="LogLevel"/>, and notify any
73-
''' listeners if <paramref name="LogToDisplay"/> is specified.
115+
''' Write the <paramref name="message"/> to the Debug tracer is debugging, into the <see cref="LastEventsList" />
116+
''' for report generating, to the <see cref="LogFile"/> if appropriate, and notify any listeners if
117+
''' <paramref name="LogToDisplay"/> is specified.
74118
''' </summary>
75119
''' <param name="message">The raw information that needs to be recorded.</param>
76120
''' <param name="LvlError">How important the information is.</param>
@@ -100,7 +144,7 @@ Public Class Logger
100144

101145
Me.LastEventsList.Add(FinalMsg)
102146

103-
If Me.WriteToFile AndAlso Me.LogLevel >= LvlError Then
147+
If IsWritingToFile AndAlso Me.LogLevel >= LvlError Then
104148
LogFile.WriteLine(FinalMsg)
105149
End If
106150
'If LvlError = LogLvl.LOG_NOTICE Then

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

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

10+
Imports System.IO
11+
1012
Public Module WinNUT_Globals
13+
#Region "Properties"
14+
' The directory where volatile appdata is stored.
15+
ReadOnly Property ApplicationData() As String
16+
Get
17+
#If DEBUG Then
18+
' If debugging, keep any generated data next to the debug executable.
19+
Return Path.Combine(Environment.CurrentDirectory, "Data")
20+
#End If
21+
Return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\WinNUT-Client")
22+
End Get
23+
End Property
24+
#End Region
25+
1126
Public LongProgramName As String
1227
Public ProgramName As String
1328
Public ProgramVersion As String
1429
Public ShortProgramVersion As String
1530
Public GitHubURL As String
1631
Public Copyright As String
17-
Public Directory_AppData As String
1832
Public IsConnected As Boolean
19-
Public LogFile As String
33+
' Public LogFile As String
34+
' Handle application messages and debug events.
35+
Public WithEvents LogFile As Logger ' As New Logger(False, 0)
2036
Public AppIcon As Dictionary(Of Integer, System.Drawing.Icon)
2137
Public StrLog As New List(Of String)
22-
Public LogFilePath As String
38+
' Public LogFilePath As String
39+
2340
Public Sub Init_Globals()
2441
LongProgramName = My.Application.Info.Description
2542
ProgramName = My.Application.Info.ProductName
2643
ProgramVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString
2744
ShortProgramVersion = ProgramVersion.Substring(0, ProgramVersion.IndexOf(".", ProgramVersion.IndexOf(".") + 1))
2845
GitHubURL = My.Application.Info.Trademark
2946
Copyright = My.Application.Info.Copyright
30-
Directory_AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\WinNUT-Client"
31-
32-
If Not System.IO.Directory.Exists(Directory_AppData) Then
33-
My.Computer.FileSystem.CreateDirectory(Directory_AppData)
34-
End If
3547
IsConnected = False
3648

3749
'Add Main Gui's Strings
@@ -79,4 +91,14 @@ Public Module WinNUT_Globals
7991
'StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, Resources.Log_Str_12)
8092
End Sub
8193

94+
'Sub SetupAppDirectory()
95+
' If Not Directory.Exists(ApplicationData) Then
96+
' Try
97+
' Directory.CreateDirectory(ApplicationData)
98+
' Catch ex As Exception
99+
' Logger.LogTracing("Could not create application directory! Operating with reduced functionality.\n\n" & ex.ToString(),
100+
' LogLvl.LOG_ERROR, Nothing)
101+
' End Try
102+
' End If
103+
'End Sub
82104
End Module

WinNUT_V2/WinNUT_GUI/About_Gui.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Public Class About_Gui
1919
Lbl_Copyright_2019.Text = Strings.Replace(WinNUT_Globals.Copyright, "©", vbNewLine & "©")
2020
LkLbl_Github.Text = WinNUT_Globals.GitHubURL
2121
Me.Icon = WinNUT.Icon
22-
Me.LogFile = WinNUT.LogFile
22+
' Me.LogFile = WinNUT.LogFile
2323
LogFile.LogTracing("Load About Gui", LogLvl.LOG_DEBUG, Me)
2424
End Sub
2525

WinNUT_V2/WinNUT_GUI/ApplicationEvents.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

10+
Imports WinNUT_Client_Common
1011
Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params
1112

1213
Namespace My
@@ -121,7 +122,7 @@ Namespace My
121122
Crash_Report &= Msg_Error.Text & vbNewLine & vbNewLine
122123
Crash_Report &= "Last Events :" & vbNewLine
123124

124-
For Each WinNUT_Event In WinNUT.LogFile.LastEvents
125+
For Each WinNUT_Event In LogFile.LastEvents
125126
Crash_Report &= WinNUT_Event & vbNewLine
126127
Next
127128
My.Computer.Clipboard.SetText(Crash_Report)

WinNUT_V2/WinNUT_GUI/List_Var_Gui.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Public Class List_Var_Gui
1616
Private LogFile As Logger
1717
Private UPS_Name = WinNUT.Nut_Config.UPSName
1818
Private Sub List_Var_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
19-
Me.LogFile = WinNUT.LogFile
19+
' Me.LogFile = WinNUT.LogFile
2020
LogFile.LogTracing("Load List Var Gui", LogLvl.LOG_DEBUG, Me)
2121
Me.Icon = WinNUT.Icon
2222
Me.Visible = False

WinNUT_V2/WinNUT_GUI/Pref_Gui.vb

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
Imports WinNUT_Params = WinNUT_Client_Common.WinNUT_Params
1111
Imports Logger = WinNUT_Client_Common.Logger
1212
Imports LogLvl = WinNUT_Client_Common.LogLvl
13-
Imports WinNUT_Globals = WinNUT_Client_Common.WinNUT_Globals
13+
Imports WinNUT_Client_Common.WinNUT_Globals
14+
Imports System.IO
15+
1416
Public Class Pref_Gui
1517
Private IsShowed As Boolean = False
1618
Private IsSaved As Boolean = False
17-
Private LogFile As Logger
19+
1820
Private Sub Btn_Cancel_Click(sender As Object, e As EventArgs) Handles Btn_Cancel.Click
1921
LogFile.LogTracing("Close Pref Gui from Button Cancel", LogLvl.LOG_DEBUG, Me)
2022
Me.Close()
@@ -72,23 +74,13 @@ Public Class Pref_Gui
7274
LogFile.LogTracing("WinNUT Removed From Startup.", LogLvl.LOG_DEBUG, Me)
7375
End If
7476
End If
75-
If CB_Use_Logfile.Checked Then
76-
WinNUT.LogFile.WriteLog = True
77-
LogFile.LogTracing("LogFile Enabled.", LogLvl.LOG_DEBUG, Me)
78-
Else
79-
WinNUT.LogFile.WriteLog = False
80-
LogFile.LogTracing("LogFile Disabled.", LogLvl.LOG_DEBUG, Me)
81-
End If
82-
WinNUT.LogFile.LogLevel = Cbx_LogLevel.SelectedIndex
8377

84-
WinNUT.LogFile.LogTracing("Pref_Gui Params Saved", 1, Me)
85-
If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then
86-
Btn_ViewLog.Enabled = True
87-
Btn_DeleteLog.Enabled = True
88-
Else
89-
Btn_ViewLog.Enabled = False
90-
Btn_DeleteLog.Enabled = False
91-
End If
78+
LogFile.LogLevel = Cbx_LogLevel.SelectedIndex
79+
LogFile.IsWritingToFile = CB_Use_Logfile.Checked
80+
81+
LogFile.LogTracing("Pref_Gui Params Saved", 1, Me)
82+
83+
SetLogControlsStatus()
9284
WinNUT.WinNUT_PrefsChanged()
9385
Me.IsSaved = True
9486
Catch e As Exception
@@ -326,35 +318,28 @@ Public Class Pref_Gui
326318
End Sub
327319

328320
Private Sub TabControl_Options_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl_Options.Selecting
329-
If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then
330-
Btn_ViewLog.Enabled = True
331-
Btn_DeleteLog.Enabled = True
332-
Else
333-
Btn_ViewLog.Enabled = False
334-
Btn_DeleteLog.Enabled = False
321+
If TabControl_Options.SelectedTab Is Tab_Miscellanous Then
322+
SetLogControlsStatus()
335323
End If
336324
End Sub
337325

338326
Private Sub Btn_DeleteLog_Click(sender As Object, e As EventArgs) Handles Btn_DeleteLog.Click
339327
LogFile.LogTracing("Delete LogFile", LogLvl.LOG_DEBUG, Me)
340-
If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then
341-
WinNUT.LogFile.WriteLog = False
342-
My.Computer.FileSystem.DeleteFile(WinNUT_Globals.LogFilePath)
343-
WinNUT.LogFile.WriteLog = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile")
344-
Btn_ViewLog.Enabled = True
345-
Btn_DeleteLog.Enabled = True
328+
329+
If LogFile.DeleteLogFile() Then
346330
LogFile.LogTracing("LogFile Deleted", LogLvl.LOG_DEBUG, Me)
347331
Else
348-
LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me)
349-
Btn_ViewLog.Enabled = False
350-
Btn_DeleteLog.Enabled = False
332+
LogFile.LogTracing("Error deleting log file.", LogLvl.LOG_WARNING, Me)
351333
End If
334+
335+
LogFile.IsWritingToFile = WinNUT_Params.Arr_Reg_Key.Item("UseLogFile")
336+
SetLogControlsStatus()
352337
End Sub
353338

354339
Private Sub Btn_ViewLog_Click(sender As Object, e As EventArgs) Handles Btn_ViewLog.Click
355340
LogFile.LogTracing("Show LogFile", LogLvl.LOG_DEBUG, Me)
356-
If My.Computer.FileSystem.FileExists(WinNUT_Globals.LogFilePath) Then
357-
Process.Start(WinNUT_Globals.LogFilePath)
341+
If File.Exists(LogFile.LogFileLocation) Then
342+
Process.Start(LogFile.LogFileLocation)
358343
Else
359344
LogFile.LogTracing("LogFile does not exists", LogLvl.LOG_WARNING, Me)
360345
Btn_ViewLog.Enabled = False
@@ -364,7 +349,6 @@ Public Class Pref_Gui
364349

365350
Private Sub Pref_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
366351
Me.Icon = WinNUT.Icon
367-
Me.LogFile = WinNUT.LogFile
368352
LogFile.LogTracing("Load Pref Gui", LogLvl.LOG_DEBUG, Me)
369353
End Sub
370354

@@ -374,4 +358,19 @@ Public Class Pref_Gui
374358
Me.Btn_Apply.Enabled = True
375359
End If
376360
End Sub
361+
362+
''' <summary>
363+
''' Enable or disable controls to view and delete log data if it's available.
364+
''' </summary>
365+
Private Sub SetLogControlsStatus()
366+
If WinNUT_Params.Arr_Reg_Key.Item("UseLogFile") Then ' Directory.Exists(Logger.LogFolder)
367+
Btn_ViewLog.Enabled = True
368+
Btn_DeleteLog.Enabled = True
369+
Else
370+
Btn_ViewLog.Enabled = False
371+
Btn_DeleteLog.Enabled = False
372+
End If
373+
374+
LogFile.LogTracing("Setting LogControl statuses.", LogLvl.LOG_DEBUG, Me)
375+
End Sub
377376
End Class

WinNUT_V2/WinNUT_GUI/Shutdown_Gui.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Public Class Shutdown_Gui
3434

3535
Private Sub Shutdown_Gui_Load(sender As Object, e As EventArgs) Handles MyBase.Load
3636
Me.Icon = WinNUT.Icon
37-
Me.LogFile = WinNUT.LogFile
37+
' Me.LogFile = WinNUT.LogFile
3838
LogFile.LogTracing("Load ShutDown Gui", LogLvl.LOG_DEBUG, Me)
3939
Me.Grace_Timer.Enabled = False
4040
Me.Grace_Timer.Stop()

WinNUT_V2/WinNUT_GUI/Update_Gui.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Public Class Update_Gui
4646
MyBase.SetVisibleCore(False)
4747
WinNUT.NotifyIcon.Visible = False
4848
Me.Icon = WinNUT.Icon
49-
LogFile = WinNUT.LogFile
49+
' LogFile = WinNUT.LogFile
5050
VerifyUpdate()
5151
End If
5252
End Sub
@@ -136,7 +136,7 @@ Public Class Update_Gui
136136
End If
137137
End If
138138
Catch excep As Exception
139-
WinNUT.LogFile.LogTracing(excep.Message, LogLvl.LOG_ERROR, Me)
139+
LogFile.LogTracing(excep.Message, LogLvl.LOG_ERROR, Me)
140140
End Try
141141
WinNUT_Params.Arr_Reg_Key.Item("LastDateVerification") = Now.ToString
142142
WinNUT_Params.Save_Params()

0 commit comments

Comments
 (0)