ASP.NET应用程序中的文件锁定(读/写) [英] File Locking (Read/Write) in ASP.NET Application

查看:190
本文介绍了ASP.NET应用程序中的文件锁定(读/写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个ASP.NET Web应用程序.一个负责处理一些信息并写入日志文件,另一个应用程序负责读取日志并根据用户请求显示信息.

这是我写给作家的代码

public static void WriteLog(String PathToLogFile, String Message)
{
    Mutex FileLock = new Mutex(false, "LogFileMutex");
    try
    {
        FileLock.WaitOne();
        using (StreamWriter sw = File.AppendText(FilePath))
        {
            sw.WriteLine(Message);
            sw.Close();
        }    
    }
    catch (Exception ex)
    {
        LogUtil.WriteToSystemLog(ex);
    }
    finally
    {
        FileLock.ReleaseMutex();
    }
}

这是我给读者的代码:

private String ReadLog(String PathToLogFile)
{
    FileStream fs = new FileStream(
          PathToLogFile, FileMode.Open, 
          FileAccess.Read, FileShare.ReadWrite);
    StreamReader Reader = new StreamReader(fs);
    return Reader.ReadToEnd();
}

我的问题是,以上代码足以防止锁定网络花园环境吗?

脏读是可以的. 使用新的Mutex(false,"LogFileMutex")创建Mutex,关闭StreamWriter

解决方案

听起来像您尝试实现基本队列一样.为什么不使用可以确保可用性的队列.您可以将消息放入MSMQ中,然后实施Windows服务,该服务将从队列中读取并将消息推送到DB.如果对数据库的写入失败,则只需将消息保留在队列中(尽管您将要处理位置消息,因此如果失败,则由于数据不好而导致的结果不会无限循环)

这将消除所有锁定问题,并确保向读者保证交付...

I have two ASP.NET web application. One is responsible for processing some info and writing to a log file, and the other application is reponsible for reading the log file and displays the information based on user request.

Here's my code for the Writer

public static void WriteLog(String PathToLogFile, String Message)
{
    Mutex FileLock = new Mutex(false, "LogFileMutex");
    try
    {
        FileLock.WaitOne();
        using (StreamWriter sw = File.AppendText(FilePath))
        {
            sw.WriteLine(Message);
            sw.Close();
        }    
    }
    catch (Exception ex)
    {
        LogUtil.WriteToSystemLog(ex);
    }
    finally
    {
        FileLock.ReleaseMutex();
    }
}

And here's my code for the Reader :

private String ReadLog(String PathToLogFile)
{
    FileStream fs = new FileStream(
          PathToLogFile, FileMode.Open, 
          FileAccess.Read, FileShare.ReadWrite);
    StreamReader Reader = new StreamReader(fs);
    return Reader.ReadToEnd();
}

My question, is the above code enough to prevent locking in a web garden environemnt?

EDIT 1 : Dirty read is okay. EDIT 2 : Creating Mutex with new Mutex(false, "LogFileMutex"), closing StreamWriter

解决方案

Sounds like your trying to implement a basic queue. Why not use a queue that gives you guarenteed availability. You could drop the messages into an MSMQ, then implement a windows service which will read from the queue and push the messages to the DB. If the writting to the DB fails you simply leave the message on the queue (Although you will want to handle posion messages so if it fails cause the data is bad you don't end up in an infinite loop)

This will get rid of all locking concerns and give you guarenteed delivery to your reader...

这篇关于ASP.NET应用程序中的文件锁定(读/写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆