Hello,
I created a small windows service in VB.net, intended to run on a MS Windows server 2012, which uses the EventLog.WriteEntry Method to write entries to a custom log file.
All works as intended, however, I am wondering if there would be a better way to write to the event log other than following these steps for each section of code that executed:
1st - the declaring and setting a variable:
Dim myMessage As String = "Try\catch exception triggered for archiving n the test.log"
2nd - defining the severity level:
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Error
3rd - defining the event id:
Dim myApplicatinEventId As Integer = 300
4th - definign the task category:
Dim myApplicatinCategoryId As Short = 3
5th - finally writting all to the log file:
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId)
Down below is a small portion of the source code:
Try
'Checking the file size of Test.zip if more than 500Mb proceed with file deletion
Dim fileTooLarge As New FileInfo("E:\test.zip")
If fileTooLarge.Exists Then
Dim sizeInBytes As Long = fileTooLarge.Length
If sizeInBytes > 500000000 Then
fileTooLarge.Delete()
'Log message
Dim myMessage As String = "The E:\test.zip file reached its max size, the test.zip file was deleted"
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Information
Dim myApplicationEventId As Integer = 100
Dim myApplicationCategoryId As Short = 1
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicationEventId, myApplicationCategoryId)
End If
Else
End If
Catch
'Log message
Dim myMessage As String = "Try\catch exception triggered for deleting the E:\test.zip file reached its max size"
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Error
Dim myApplicatinEventId As Integer = 300
Dim myApplicatinCategoryId As Short = 3
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId)
End Try
Dim newFile As String = "E:\test.log"
Try
Dim zipPath As String = "E:\test.zip"
If File.Exists(newFile) Then
Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.CreateEntryFromFile(newFile, "test-" & Format(Date.Now, "mmddyy-hhmmss") & ".log", CompressionLevel.Optimal)
System.IO.File.Delete(newFile)
'Log message
Dim myMessage As String = "The test.log file has been added to the E:\test.zip file"
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Information
Dim myApplicationEventId As Integer = 100
Dim myApplicationCategoryId As Short = 1
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicationEventId, myApplicationCategoryId)
End Using
Else
'Log message
Dim myMessage As String = "The test.log file in the E:\test folder was not present to be zipped"
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Warning
Dim myApplicationEventId As Integer = 200
Dim myApplicationCategoryId As Short = 2
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicationEventId, myApplicationCategoryId)
End If
Catch
'Log message
Dim myMessage As String = "Try\catch exception triggered for archiving n the test.log"
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Error
Dim myApplicatinEventId As Integer = 300
Dim myApplicatinCategoryId As Short = 3
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId)
End Try
If there would be a better approach other than writing out this section over and over again, that would be great:
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Error
Dim myApplicatinEventId As Integer = 300
Dim myApplicatinCategoryId As Short = 3
EventLog12.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId)
Thank you,
John