Imports System.IO
Module LogBestandTextMapper
Const AantalLogs As Integer = 5
Dim _busy As Boolean
Private _currentLogPath As String = ""
Public ReadOnly Property CurrentLogPath() As String
Get
Return _currentLogPath
End Get
End Property
'''
''' Wegschrijven van entry in het logbestand.
'''
''' De naam van de databank
''' Het bericht
''' Ofdat het stuk code is voltooid
Public Sub WriteToFile(databank As String, logMessage As String, Optional voltooid As Boolean = False)
'Controleren of er nog geen log bestand was gemaakt.
If Not _busy Then
'Geen log bestand was reeds aanwezig, aanmaken.
StartNewLog()
'Bijhouden dat nieuw log bestand is aangemaakt.
_busy = True
End If
Try
'Stream writer initialiseren met pad naar het log bestand. Tekst wordt niet overschreven, maar toegevoegd.
Dim streamWriter = My.Computer.FileSystem.OpenTextFileWriter(_currentLogPath, True)
'De tekst die wordt weggeschreven opbouwen: datum databank naam status bericht.
Dim logText = String.Format("{0,-23}{1,-10}{3,-13}{2}", Now.ToString("yyyy-MM-dd HH:mm:ss"), databank, logMessage, If(voltooid, "VOLTOOID!", ""))
'Wegschrijven.
streamWriter.WriteLine(logText)
'De stream writer sluiten.
streamWriter.Close()
Catch ex As Exception
Throw New Exception($"Log bestand kon niet worden gevuld.{vbNewLine}{ex.Message}")
End Try
End Sub
Private Sub StartNewLog()
Try
'Hoofding opbouwen.
Dim hoofding = String.Format("{0,-23}{1,-10}{3,-13}{2}", "Datum", "Databank", "Bericht", "Status")
'Nieuw log bestand aanmaken en pad er naartoe opslaan.
_currentLogPath = MaakNieuweLogFile()
'Streamwriter initialiseren.
Dim streamWriter = My.Computer.FileSystem.OpenTextFileWriter(_currentLogPath, False)
'Hoofding wegschrijven.
streamWriter.WriteLine(hoofding)
'Stream sluiten.
streamWriter.Close()
Catch ex As Exception
Throw New Exception($"Log bestand kon niet worden aangemaakt.{vbNewLine}{ex.Message}")
End Try
End Sub
Public Function GeefPathLaatsteLogFile() As String
'Variabelen declareren.
Dim bestandsnaam as String = String.Empty
Dim laatsteWijzigingTijdstip As Date
'Log map opvragen.
dim pathLogFolder = GetPathLogFolder()
'Alle bestanden hierin overlopen.
For each fileName in Directory.EnumerateFiles(pathLogFolder)
'Pad naar bestand opbouwen.
dim filePath = Path.Combine(pathLogFolder, fileName)
'Tijdstip laatste wijziging.
dim lastChanged = System.IO.File.GetLastWriteTimeUtc(filePath)
'Controleren of de datum van dit bestand recenter is.
If bestandsnaam is String.Empty OrElse (lastChanged > laatsteWijzigingTijdstip) Then
'Zo ja, registreer dit bestand.
bestandsnaam = fileName
laatsteWijzigingTijdstip = lastChanged
End If
Next
Return bestandsnaam
End Function
Private Function MaakNieuweLogFile() As String
Dim logFolderPath As String = GetPathLogFolder()
Dim vorigeLogFileAanwezig As Boolean
Dim volledigPadVorigeLogFile As String
For teller As Integer = 1 To AantalLogs
volledigPadVorigeLogFile = Path.Combine(logFolderPath, "log" & teller & teller & ".txt")
If System.IO.File.Exists(volledigPadVorigeLogFile) Then
vorigeLogFileAanwezig = True
'als er al een log bestaat, hernoemen en een nieuwe aanmaken
My.Computer.FileSystem.RenameFile(volledigPadVorigeLogFile, "log" & teller & ".txt")
Dim logIndex As Integer = ((teller Mod 5) + 1)
Dim padNieuweLogFile As String = Path.Combine(logFolderPath, "log" & logIndex & ".txt")
'kijken of de volgende log al bestaat, indien wel, verwijderen en nieuwe aanmaken
If System.IO.File.Exists(padNieuweLogFile) Then
My.Computer.FileSystem.DeleteFile(padNieuweLogFile)
End If
Return Path.Combine(logFolderPath, "log" & logIndex & logIndex & ".txt")
End If
Next
If Not vorigeLogFileAanwezig Then
Return Path.Combine(logFolderPath, "log" & 1 & 1 & ".txt")
End If
Throw New Exception("Er is iets fout gelopen bij het opvragen van het Path voor de log bestanden")
End Function
Public Function GetPathLogFolder() As String
'Opvragen van het pad naar het bureaublad
Dim logFolderPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim logfolder As String = "TAOR REWORK LOGS"
logFolderPath = Path.Combine(logFolderPath, logfolder)
'als de folder nog niet bestaat, aanmaken
If (Not System.IO.Directory.Exists(logFolderPath)) Then
System.IO.Directory.CreateDirectory(logFolderPath)
End If
Return logFolderPath
End Function
End Module