Imports System.IO

Public Class clsLogger
    Private Shared LogPath As String
    Public Shared LogFilename As String
    Private Shared logErr_name As String
    Private Shared log_string As String
    Public Shared Function Init(ByVal speicherort As String, ByVal prefix As String)
        Try
            'Den Speicherort festlegen
            SetSpeicherort()
            Dim logf_name As String = LogPath & "\" & prefix & System.DateTime.Now.ToString("yyyy_MM_dd") & ".txt"
            logErr_name = LogPath & "\" & "ErrorLog_" & System.DateTime.Now.ToString("yyyy_MM_dd") & ".txt"
            Dim anz As Integer = 1
            Do While File.Exists(logf_name)
                Dim info As New FileInfo(logf_name)
                Dim length As Long = info.Length
                If length > 5000000 Then
                    logf_name = IO.Path.GetDirectoryName(logf_name)
                    logf_name = logf_name & "\" & prefix & System.DateTime.Now.ToString("yyyy_MM_dd") & "(" & anz.ToString & ").txt"
                    anz = anz + 1
                Else
                    Exit Do
                End If
            Loop
            LogFilename = logf_name
            Try
                'Veruch das Log zu öffnen
                Dim fs As FileStream = New FileStream(LogFilename, FileMode.OpenOrCreate, FileAccess.Write)
                ' --- Stream öffnen
                Dim w As StreamWriter = New StreamWriter(fs)
                ' --- Anfügen am Ende
                w.BaseStream.Seek(0, SeekOrigin.End)
                ' --- Zeilen schreiben
                w.WriteLine(" ")
                ' --- Writer und Stream schließen
                w.Close()
                fs.Close()
            Catch ex As Exception
                LogEscalation_Error("The Logfile could not be created - Error: " & ex.Message)
                Return False
            End Try
            'Alles Ok 
            Return True
        Catch ex As Exception
            LogEscalation_Error("Unexpected Error in init Logger - Error: " & ex.Message)
            Return False
        End Try

    End Function
    Private Shared Sub LogEscalation_Error(msg As String)
        Try
            'Veruch das Log zu öffnen
            Dim fs As FileStream = New FileStream(logErr_name, FileMode.OpenOrCreate, FileAccess.Write)
            ' --- Stream öffnen
            Dim w As StreamWriter = New StreamWriter(fs)
            ' --- Anfügen am Ende
            w.BaseStream.Seek(0, SeekOrigin.End)
            ' --- Zeilen schreiben
            w.WriteLine(msg)
            ' --- Writer und Stream schließen
            w.Close()
            fs.Close()
        Catch ex As Exception

        End Try
    End Sub
    ' legt den Speicherort fest
    Public Shared Sub SetSpeicherort()
        Dim f As New IO.DirectoryInfo(My.Application.Info.DirectoryPath & "\Log")
        If f.Exists = False Then
            IO.Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\Log")
        End If
        LogPath = My.Application.Info.DirectoryPath & "\Log\"
    End Sub
    Public Shared Sub Add(ByVal text As String, ByVal _error As Boolean, Optional ByVal Funktion As String = "")
        Dim msg As String
        If log_string <> "" Then
            log_string &= vbNewLine
        End If
        'Präfixe schreiben
        If _error = True And Funktion <> "" Then
            msg = ">> Achtung Fehler in Funktion '" & Funktion & "'" & vbNewLine & "Fehlermeldung: "
        ElseIf _error = True Then
            msg = ">> Achtung Fehler:" & vbNewLine & "Fehlermeldung: "
        End If
        'Präfix und Meldung zusammenstellen
        msg &= text
        log_string &= msg
    End Sub
    Public Shared Sub AddError(ByVal error_string As String, Optional ByVal Funktion As String = "")
        Try
            'Zuerst mal die Details schreiben
            WriteLog()
            'Nun den eigentlichen Fehler loggen
            Dim msg As String
            'Präfixe schreiben
            If Funktion <> "" Then
                msg = ">> Achtung Fehler in Funktion '" & Funktion & "'" & vbNewLine & "Fehlermeldung: "
            Else
                msg = ">> Achtung Fehler:" & vbNewLine & "Fehlermeldung: "
            End If
            'Präfix und Meldung zusammenstellen
            msg &= error_string
            'Veruch das Log zu öffnen
            Dim fs As FileStream = New FileStream(LogFilename, FileMode.OpenOrCreate, FileAccess.Write)
            ' --- Stream öffnen
            Dim w As StreamWriter = New StreamWriter(fs)
            ' --- Anfügen am Ende
            w.BaseStream.Seek(0, SeekOrigin.End)
            ' --- Zeilen schreiben
            w.WriteLine(msg)
            ' --- Writer und Stream schließen
            w.Close()
            fs.Close()
        Catch ex As Exception
            LogEscalation_Error("Unexpected Error in AddError - Error: " & ex.Message)
        End Try
        
    End Sub

    Public Shared Sub AddDetailLog(ByVal text As String)
        Try
            If LOG_ERRORS_ONLY = False Then
                If log_string <> "" Then
                    log_string &= vbNewLine
                End If
                log_string &= ">> " & text
            End If
        Catch ex As Exception
            LogEscalation_Error("Unexpected Error in AddDetailLog - Error: " & ex.Message)
        End Try
    End Sub

    Public Shared Sub WriteLog()
        Try
            'Veruch das Log zu öffnen
            Dim fs As FileStream = New FileStream(LogFilename, FileMode.OpenOrCreate, FileAccess.Write)
            ' --- Stream öffnen
            Dim w As StreamWriter = New StreamWriter(fs)
            ' --- Anfügen am Ende
            w.BaseStream.Seek(0, SeekOrigin.End)
            ' --- Zeilen schreiben
            w.WriteLine(log_string)
            ' --- Writer und Stream schließen
            w.Close()
            fs.Close()
            SetSpeicherort()
            'Den Meldungsstring wieder leeren
            log_string = ""
        Catch ex As Exception
            LogEscalation_Error("Unexpected Error in WriteLog - Error: " & ex.Message)
        End Try
    End Sub
End Class
