0

ログ ファイル (.txt) のサイズがしきい値レベル (たとえば 5MB) に達したときに、ログ ファイル (.txt) を自動的にバックアップする方法。バックアップ ファイル名は (log_file_name)_(system_date) のようにし、元のログ ファイルは消去する必要があります (0 KB)。

助けてください。前もって感謝します。

4

1 に答える 1

0

length() を使用してログ ファイルのサイズを確認します。次に、extendLogFile() 関数を呼び出して 5 MB より大きいかどうかを確認します。

これは、Java に簡単に変換できる C# コードです。

サイズチェック:

if (size > 400 * 100 * 100)
{
   extendLogFile(Path);
}

古いログ ファイルをアーカイブ ディレクトリにコピーし、新しいログ ファイルを作成します。

private static void extendLogFile(string lPath)
{
        string name = lPath.Substring(0, lPath.LastIndexOf("."));
        string UniquName = GenerateUniqueNameUsingDate(); // create a unique name for old log files like '12-04-2013-12-43-00'

        string ArchivePath = System.IO.Path.GetDirectoryName(lPath) + "\\Archive";
        if (!string.IsNullOrEmpty(ArchivePath) && !System.IO.Directory.Exists(ArchivePath))
        {
            System.IO.Directory.CreateDirectory(ArchivePath);
        }

        string newName = ArcivePath + "\\" + UniquName;

        if (!File.Exists(newName))
        {

            File.Copy(lPath, newName + ".txt");

            using (FileStream stream = new FileStream(lPath, FileMode.Create))
            using (TextWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine("");
            }
        }


 }
于 2013-05-08T12:46:43.040 に答える