user189320
user189320

Reputation:

How to handle concurrent file access with a filestream/streamwriter?

I am writing an audit file that is writing the username, time, and the old/changed values of several variables in the application for each user when they use my application. It is using a FileStream and StreamWriter to access the audit file. All audits for each user will be written to the same file.

The issue is that when two users are updating this audit file at the same time, the "old value" of each of the variable is mixing up between the users. Why is this and how can you solve the concurrency problem here?

Some code, shortened for brevity...

Dim fs As FileStream
Dim w As StreamWriter

Public Sub WriteAudit(ByVal filename As String, ByVal username As String, ByVal oldAddress As String, ByVal newAddress As String, ByVal oldCity As String, ByVal newCity As String)
    Dim now As DateTime = DateTime.Now
    Dim audit As String = ""
    audit += now + "," + username + "," + oldAddress + "," + newAddress + "," + oldCity + "," + newCity

    fs = New FileStream(filename, FileMode.Append)
    w = New StreamWriter(fs)
    w.WriteLine(audit)
    w.Close()
    fs.Close()
End Sub

This lives in an AuditLogger class, which is referenced via an instance variable (re-allocated each time the function is accessed).

Upvotes: 4

Views: 10721

Answers (3)

Dave Mateer
Dave Mateer

Reputation: 17946

Refactor the application so that you do not have to create a new instance of the AuditLogger class each time. Use the singleton pattern directly, or a dependency-injection framework to use the same instance throughout the application.

From there, the implementation is much easier: surround the write operations with lock statements, or use the TextWriter.Synchronized as has been mentioned in Robert's answer.

This post may be relevant:

Upvotes: 2

Robert Harvey
Robert Harvey

Reputation: 180788

You might try this:

TextWriter tw = TextWriter.Synchronized(File.AppendText(filePath));

The File.AppendText() method returns a StreamWriter object, which the TextWriter.Synchronized() method wraps to create a thread-safe TextWriter which can be used just like a StreamWriter.

Upvotes: 2

Goyuix
Goyuix

Reputation: 24340

A couple of ways:

First, use a shared database not a file. Even a simple access database can handle multiple users much more gracefully than a file on disk.

Second, can you use a separate file for each user? maybe store it in their %APPDATA% folder? Or maybe on a network share somewhere?

Upvotes: 0

Related Questions