DanielSimons
9/12/2017 - 7:46 AM

Logging

Creates a logfile in root folder.

Imports Microsoft.VisualBasic.CompilerServices

Private Logfile As String = Application.StartupPath & "\Logfile.log"

Private Sub WriteToLog_Append(ByVal strMessage As String)
        If System.IO.File.Exists(Logfile) = False Then
            System.IO.File.Create(Logfile).Dispose()
        End If
        Dim streamwriter1 As System.IO.StreamWriter = Nothing
        Try
            streamwriter1 = New System.IO.StreamWriter(Me.Logfile, True)
            streamwriter1.WriteLine(strMessage)

        Catch exception1 As Exception

        Finally
            If (streamwriter1 IsNot Nothing) Then
                streamwriter1.Close()
            End If
        End Try
    End Sub

'Example for a logging chooser 
Private Sub WriteErrorInLog(ByVal name As String, msg As String)
        If msg = "Status1" Then
            WriteToLog_Append(Conversions.ToString(DateTime.Now) & " " & name & " " & "status1 etc")
        ElseIf msg = "Status2" Then
            WriteToLog_Append(Conversions.ToString(DateTime.Now) & " " & name & " " & "status2 etc")
        End If
End Sub

'Function call
WriteErrorInLog("test","Status1")