3

StreamWriterを使用した同時ファイルアクセスの処理について話している投稿を見つけました。

問題は、回答がファイルにアクセスされているシナリオではなく、複数のプロセスを管理していることです。

すぐにそれを伝えましょう:

  • 複数のアプリケーションがあります
  • データベースに一元化されたロギングシステムが必要です
  • データベースに障害が発生した場合、ファイルシステムログでフォールバックする必要があります

複数のアプリケーション(プロセス)がそのファイルに書き込もうとする、既知の同時実行シナリオがあります。これは、少し遅れて書き込みを再試行することで管理できます。ただし、セキュリティエラーまたはファイル名構文エラーの場合は、再試行したくありません。

コードはここにあります:

// 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);

私の唯一の質問は、同時実行書き込みの場合に発生する例外のタイプです。私はすでに物事が違ったやり方でできることを知っています。いくつかの考慮事項を追加しましょう:

  • いいえ、そのシナリオではNLogを使用したくありません
  • はい、インプロセス同時実行のためにIOC+Mutexで同時実行を処理します
  • はい、本当にすべてのログを同じファイルに書き込みたいです
4

1 に答える 1

2

テキスト付きになりIOExceptionます:

「別のプロセスによって使用されているため、プロセスはファイル'{0}'にアクセスできません。」

これは単純なアプローチです。

 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;
    }
于 2010-12-01T13:23:47.543 に答える