Hey guys, hope everyone is staying covid safe :)
I have been looking around for examples but have yet to come across anything close to what I am trying to do. As my application grows I'm finding myself adding more and more classes reading or writing files. I'm guessing it would make more sense to have a centralized class for writing and a centralized class for reading.
The application has a handful of different files that need to be outputted to an occasional read. One major issue is as the project grows the main log file that gets written to every second or so is facing race conditions from the different tasks trying to access the file. I have seen and tried things such as enterwritelock but don't think I was implementing them correctly so scrapped that class.
So I was hoping there was an example on here or on the net keeping in mind I have searched for both that can cover a centralized writing class where any file and be passed for writing but also handles race conditions from the tasks. I have seen someone mention a queue of some sort, waithandle etc but quite frankly it has boggled me.
I always do my homework and have been pondering creating this thread for weeks because it's a thread without code. But there is really no point posting code to file.readalllines etc. Actually, I guess I could post this small example of what I was doing with the lock but obviously, this is not a file management system
So please, any insight into the best way how a professional company would centralize it's file writing management would be amazing.
I have been looking around for examples but have yet to come across anything close to what I am trying to do. As my application grows I'm finding myself adding more and more classes reading or writing files. I'm guessing it would make more sense to have a centralized class for writing and a centralized class for reading.
The application has a handful of different files that need to be outputted to an occasional read. One major issue is as the project grows the main log file that gets written to every second or so is facing race conditions from the different tasks trying to access the file. I have seen and tried things such as enterwritelock but don't think I was implementing them correctly so scrapped that class.
So I was hoping there was an example on here or on the net keeping in mind I have searched for both that can cover a centralized writing class where any file and be passed for writing but also handles race conditions from the tasks. I have seen someone mention a queue of some sort, waithandle etc but quite frankly it has boggled me.
I always do my homework and have been pondering creating this thread for weeks because it's a thread without code. But there is really no point posting code to file.readalllines etc. Actually, I guess I could post this small example of what I was doing with the lock but obviously, this is not a file management system
vb Code:
Imports System.IO Imports System.Threading Namespace Classes.BotOutput.ChatLogHelper Friend Class LogChat ''' <summary> ''' Represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing. ''' </summary> Private Shared ReadOnly ReadWriteLock As New ReaderWriterLockSlim() ''' <summary> ''' Writes information about chat content to the chat log. ''' </summary> ''' <param name="message">The entry to log.</param> Friend Shared Sub Log(message As String) If message.StartsWith(".") Then Exit Sub End If Dim entry = message ReadWriteLock.EnterWriteLock() Try Using sw As StreamWriter = File.AppendText(GetLogsFileName) sw.WriteLine(entry) sw.Close() End Using Catch ex As Exception LogException(ex, "LogChat()") Finally ReadWriteLock.ExitWriteLock() End Try End Sub End Class End Namespace
So please, any insight into the best way how a professional company would centralize it's file writing management would be amazing.