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で同時実行を処理します
- はい、本当にすべてのログを同じファイルに書き込みたいです