Jump to content
Maximus

VB.NET mtLogWriter

Recommended Posts

Posted

Multithreading Log Writer

Public Class mtLogWriter
    Implements IDisposable

    Private LogWriters As Dictionary(Of Integer, IO.StreamWriter)
    Public WritersCount As Integer = 0

    Public Sub New()
        LogWriters = New Dictionary(Of Integer, IO.StreamWriter)
        WritersCount = 0
    End Sub

    Public Function Add(ByVal path As String, ByVal append As Boolean) As Boolean
        Try
            LogWriters.Add(WritersCount, New IO.StreamWriter(path, append))
            WritersCount += 1
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Sub Remove(ByVal intLogWriter As Integer)
        If LogWriters.ContainsKey(intLogWriter) Then
            SyncLock LogWriters.Item(intLogWriter)
                LogWriters.Item(intLogWriter).Close()
                LogWriters.Item(intLogWriter).Dispose()
                LogWriters.Remove(intLogWriter)
            End SyncLock
        End If
    End Sub

    Public Sub WriteLine(ByVal intLogWriter As Integer, ByVal contents As String, Optional ByVal flush As Boolean = False)
        SyncLock LogWriters.Item(intLogWriter)
            LogWriters.Item(intLogWriter).WriteLine(contents)
            If flush = True Then LogWriters.Item(intLogWriter).Flush()
        End SyncLock
    End Sub

    Public Sub Write(ByVal intLogWriter As Integer, ByVal contents As String, Optional ByVal flush As Boolean = False)
        SyncLock LogWriters.Item(intLogWriter)
            LogWriters.Item(intLogWriter).Write(contents)
            If flush = True Then LogWriters.Item(intLogWriter).Flush()
        End SyncLock
    End Sub

    Public Sub Flush()
        For int As Integer = 0 To LogWriters.Count - 1
            SyncLock LogWriters.Item(int)
                LogWriters.Item(int).Flush()
            End SyncLock
        Next
    End Sub

    Public Sub Flush(ByVal intLogWriter As Integer)
        SyncLock LogWriters.Item(intLogWriter)
            LogWriters.Item(intLogWriter).Flush()
        End SyncLock
    End Sub

    Public Sub Close()
        For int As Integer = 0 To LogWriters.Count - 1
            SyncLock LogWriters.Item(int)
                LogWriters.Item(int).Close()
                LogWriters.Item(int).Dispose()
                LogWriters.Remove(int)
            End SyncLock
        Next
    End Sub

    Public Sub Close(ByVal intLogWriter As Integer)
        SyncLock LogWriters.Item(intLogWriter)
            LogWriters.Item(intLogWriter).Close()
            LogWriters.Item(intLogWriter).Dispose()
            LogWriters.Remove(intLogWriter)
        End SyncLock
    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean

    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                For int As Integer = 0 To LogWriters.Count - 1
                    If LogWriters.Item(int) IsNot Nothing Then
                        LogWriters.Item(int).Close()
                        LogWriters.Item(int).Dispose()
                    End If
                Next
                LogWriters.Clear()
                LogWriters = Nothing
            End If
        End If
        disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
    End Sub
#End Region

End Class

Test :

Module Module1
    Private logw As LogWriter.mtLogWriter
    Private WaitMe As Boolean = True
    Sub Main()
        logw = New LogWriter.mtLogWriter
        For i As Integer = 0 To 20
            logw.Add("test/" & i & ".txt", False)
            logw.WriteLine(i, "First line")
            Dim th As New System.Threading.Thread(AddressOf WriteTest) ' Test
            th.IsBackground = True
            th.Start(i)
        Next
        'Generate error
        If logw.Add("test/2.txt", True) = False Then
            Console.WriteLine("Could not open writer")
        End If
        'End
        WaitMe = False
        Console.ReadLine()
        logw.Dispose()
        Console.ReadLine()
    End Sub

    Public Sub WriteTest(ByVal int As Object)
        While WaitMe = True
            System.Threading.Thread.Sleep(100)
        End While
        Dim Log_int As Integer = CInt(int)
        For i As Integer = 0 To 2000
            logw.WriteLine(Log_int, "MT line : " & i.ToString)
        Next
        logw.Flush(Log_int)
        Console.WriteLine("Thread " & Log_int & " finish !")
    End Sub
End Module

//Update

  • Upvote 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...