1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- using System.Windows.Forms;
- namespace BEMSDataGateway
- {
- class LogFileCreate
- {
- public string GetDateTime()
- {
- DateTime NowDate = DateTime.Now;
- return NowDate.ToString("yyyy-MM-dd HH:mm:ss") + ":" + NowDate.Millisecond.ToString("000");
- }
- public void Log(string str)
- {
- string FilePath = Application.StartupPath + @"\Logs\Log" + DateTime.Today.ToString("yyyyMMdd") + ".log";
- string DirPath = Application.StartupPath + @"\Logs";
- string temp;
- DirectoryInfo di = new DirectoryInfo(DirPath);
- FileInfo fi = new FileInfo(FilePath);
- if (di.Exists != true) //로그 폴더 없을때, 로그 폴더 생성
- {
- Directory.CreateDirectory(DirPath);
- }
- if (fi.Exists != true) //로그 파일 없을때, 로그 파일 생성
- {
- using (StreamWriter sw = new StreamWriter(FilePath))
- {
- temp = string.Format("[{0}] : {1}", GetDateTime(), str);
- sw.WriteLine(temp);
- sw.Close();
- sw.Dispose();
- }
- }
- else
- {
- using (StreamWriter sw = File.AppendText(FilePath)) // 로그 파일 있으면, AppendText
- {
- temp = string.Format("[{0}] : {1}", GetDateTime(), str);
- sw.WriteLine(temp);
- sw.Close();
- sw.Dispose();
- }
- }
- DeleteServiceLogByDay(10); //현재일로부터 10일 이전 로그 파일 삭제
- }
- internal void DeleteServiceLogByDay(int keepDay)
- {
- try
- {
- string DirPath = Application.StartupPath + @"\Logs";
- DirectoryInfo di = new DirectoryInfo(DirPath);
- foreach (FileInfo file in di.GetFiles())
- {
- if (file.Extension == ".log")
- {
- if (file.CreationTime < DateTime.Now.AddDays(-(keepDay)))
- {
- file.Delete();
- }
- }
- }
- di = null;
- }
- catch (Exception ex)
- {
- if (Form1.Enable_EnglishMode)//영어
- {
- Log("[Log.cs] Errors in log file deletion : " + ex);
- }
- else//한글
- {
- Log("[Log.cs] 로그 파일 삭제 에러 발생 : " + ex);
- }
- }
- }
- }
- }
|