77'
88' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99
10+ Imports System.IO
11+
1012Public 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 WriteLogValue 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 .WriteLogValue = 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 .WriteLogValue
48+ Return Not (LogFile Is Nothing )
5249 End Get
53- Set ( ByVal Value As Boolean )
54- Me .WriteLogValue = Value
55- If Not Me .WriteLogValue 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,18 +79,62 @@ 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+
114+ ''' <summary>
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.
118+ ''' </summary>
119+ ''' <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>
122+ ''' <param name="LogToDisplay">A user-friendly, translated string to be shown.</param>
70123 Public Sub LogTracing( ByVal message As String , ByVal LvlError As Int16, sender As Object , Optional ByVal LogToDisplay As String = Nothing )
71124 Dim Pid = TEventCache.ProcessId
72125 Dim SenderName = sender.GetType.Name
73126 Dim EventTime = Now.ToLocalTime
74127 Dim FinalMsg = EventTime & " Pid: " & Pid & " " & SenderName & " : " & message
75128
76129 'Update LogFilePath to make sure it's still the correct path
77- WinNUT_Globals.LogFilePath = Me .LogFile.FullLogFileName
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
78134
79135 ' Always write log messages to the attached debug messages window.
80136# If DEBUG Then
81- Debug.WriteLine(FinalMsg)
137+ Debug.WriteLine(FinalMsg)
82138# End If
83139
84140 'Create Event in EventList in case of crash for generate Report
@@ -88,7 +144,7 @@ Public Class Logger
88144
89145 Me .LastEventsList.Add(FinalMsg)
90146
91- If Me .WriteLogValue AndAlso Me .LogLevel >= LvlError Then
147+ If IsWritingToFile AndAlso Me .LogLevel >= LvlError Then
92148 LogFile.WriteLine(FinalMsg)
93149 End If
94150 'If LvlError = LogLvl.LOG_NOTICE Then
0 commit comments