LoginSignup
0
0

More than 3 years have passed since last update.

ファイル名が日時のログ

Posted at

現在時刻(OS準拠)を取得してログファイル名に

DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss") で西暦から秒まで取得できる.西暦など省略もできる.
System.Text.Encoding.GetEncoding("shift_jis"))でテキストのエンコード方式を指定できる.
下記の例ではexeのある階層に"yyyy.MM.dd.HH.mm.ss.log"の空ファイルができる.

        using TextWriter writerSync = TextWriter.Synchronized(new StreamWriter(DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss") + ".log", false, System.Text.Encoding.GetEncoding("shift_jis")));
        writerSync.WriteLine(DateTime.Now.ToString("HH.mm.ss:")+"ここにログを記述");

ログを7z(7zip)で圧縮する場合

階層内のログファイルをまとめて圧縮する.
ファイル名は"yyyy.MM.dd.HH.mm.ss.7z"になる.
以下のURLから"7z.exe"を入手しexeと同じ場所に配置する必要がある.
https://sevenzip.osdn.jp/download.html
-sdelは圧縮したら元のファイルを削除
-mx9は圧縮レベルで0は無圧縮(0-9)
".7zを".zipに変えるとzipになる

        ExecuteAnotherApp("7z.exe", "a " + DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss") + ".7z *.log -sdel -mx9", false, true);

        private void ExecuteAnotherApp(in string FileName, in string Arguments, bool UseShellExecute, bool CreateNoWindow) {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo {
                FileName = FileName, Arguments = Arguments,
                UseShellExecute = UseShellExecute,
                CreateNoWindow = CreateNoWindow    // コンソール・ウィンドウを開かない
            }).WaitForExit();
        }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0