在与StreamReader的并发文件访问的情况下引发的异常 [英] Raised exception in the case of concurrent file access with StreamReader

查看:195
本文介绍了在与StreamReader的并发文件访问的情况下引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个后谈论处理并发文件访问与StreamWriter的

现在的问题是,答案不管理该文件被访问,但多进程的场景。

The problem is that the answers do not manage the scenario where the file is being accessed but multiple processes.

让我们告诉它不久:


  • 我有多个应用程序

  • 我需要一个集中的日志系统数据库

  • 如果数据库失败了,我需要一个文件系统日志后备

有是一个已知的并发情况下,多个应用程序(进程)将尝试在该文件中写入。
这可以通过重新尝试一个短暂的延迟后的写入进行管理。
,而我不想OT重新尝试,如果它是一个安全错误或文件名语法错误

There is a known concurrency scenario, where multiple applications (processes) will try to write in that file. This can be managed by re-attempt the writing after a short delay. But I don't want ot reattempt if it's a security error or filename syntax error.

中的代码是在这里:

// true if an access error occured
bool accessError = false;
// number fo writing attemps
int attempts = 0;

do
{
    try
    {
        // open the file
        using (StreamWriter file = new StreamWriter(filename, true))
        {
            // write the line
            file.WriteLine(log);
            // success
            result = true;
        }
    }
        /////////////// access errors ///////////////
    catch (ArgumentException)
    {
        accessError = true;
    }
    catch (DirectoryNotFoundException)
    {
        accessError = true;
    }
    catch (PathTooLongException)
    {
        accessError = true;
    }
    catch (SecurityException)
    {
        accessError = true;
    }
        /////////////// concurrent writing errors ///////////////
    catch (Exception)
    {
        // WHAT EXCEPTION SHOULD I CATCH HERE ?
        // sleep before retrying
        Thread.Sleep(ConcurrentWriteDelay);
    }
    finally
    {
        attempts++;
    }
    // while the number of attemps has not been reached
} while ((attempts < ConcurrentWriteAttempts)
            // while we have no access error
            && !accessError
            // while the log is not written
            && !result);



我唯一的问题是,异常的类型将在提高案例并发,书写。我已经知道的东西可以分别进行。让我补充几个方面的考虑:

My only question is the type of exception that will be raised in the case of concurrency writting. I already know things can be done differently. Let me add a few considerations :


  • 不,我不希望在场景中使用NLOG

  • 是我处理与国际奥委会+互斥并发的过程中的并发

  • 是的,我真的希望所有登录同一个文件

  • 写入
  • No, I don't want to use NLog in that scenario
  • Yes I handle concurrency with IOC + Mutex for the in-process concurrency
  • Yes I really want all log to be written in the same file

推荐答案

这将是一个 IOException异常文本:

该进程无法访问文件,因为它正被另一个进程使用{0}。

"The process cannot access the file '{0}' because it is being used by another process."

这是一个简单的方法:

 static bool LogError(string filename, string log)
    {
        const int MAX_RETRY = 10;
        const int DELAY_MS = 1000; // 1 second
        bool result = false;
        int retry = 0;
        bool keepRetry = true;
        while (keepRetry && !result && retry < MAX_RETRY )
        {
            try
            {
                using (StreamWriter file = new StreamWriter(filename, true))
                {
                    // write the line
                    file.WriteLine(log);
                    // success
                    result = true;
                }
            }
            catch (IOException ioException)
            {
                Thread.Sleep(DELAY_MS);
                retry++; 
            }
            catch (Exception e)
            {

                keepRetry = false;
            }

        }
        return result;
    }

这篇关于在与StreamReader的并发文件访问的情况下引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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