Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16039

centralized file reading and writing

$
0
0
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

vb Code:
  1. Imports System.IO
  2. Imports System.Threading
  3.  
  4. Namespace Classes.BotOutput.ChatLogHelper
  5.  
  6.     Friend Class LogChat
  7.  
  8.         ''' <summary>
  9.         ''' Represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing.
  10.         ''' </summary>
  11.         Private Shared ReadOnly ReadWriteLock As New ReaderWriterLockSlim()
  12.  
  13.         ''' <summary>
  14.         ''' Writes information about chat content to the chat log.
  15.         ''' </summary>
  16.         ''' <param name="message">The entry to log.</param>
  17.         Friend Shared Sub Log(message As String)
  18.             If message.StartsWith(".") Then
  19.                 Exit Sub
  20.             End If
  21.  
  22.             Dim entry = message
  23.  
  24.             ReadWriteLock.EnterWriteLock()
  25.  
  26.             Try
  27.                 Using sw As StreamWriter = File.AppendText(GetLogsFileName)
  28.                     sw.WriteLine(entry)
  29.                     sw.Close()
  30.                 End Using
  31.             Catch ex As Exception
  32.                 LogException(ex, "LogChat()")
  33.             Finally
  34.                 ReadWriteLock.ExitWriteLock()
  35.             End Try
  36.         End Sub
  37.     End Class
  38. End Namespace

So please, any insight into the best way how a professional company would centralize it's file writing management would be amazing.

Viewing all articles
Browse latest Browse all 16039

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>