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

情到浓时终转凉″ 提交于 2019-12-19 08:57:02

问题


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).


回答1:


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.




回答2:


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:

  • Dependency injection and logging interfaces



回答3:


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?



来源:https://stackoverflow.com/questions/1882637/how-to-handle-concurrent-file-access-with-a-filestream-streamwriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!