1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- using System.Windows.Forms;
- namespace DataGateWayProject
- {
- 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);
-
- try
- {
- 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();
- }
- }
- else
- {
- using (StreamWriter sw = File.AppendText(FilePath))
- {
- temp = string.Format("[{0}] : {1}", GetDateTime(), str);
- sw.WriteLine(temp);
- sw.Close();
- }
- }
- }
- catch
- {
-
- }
- DeleteServiceLogByDay(7); //현재일로부터 7일 이전 로그 파일 삭제
- }
- internal void DeleteServiceLogByDay(int keepDay)
- {
- try
- {
- //string FilePath = Application.StartupPath + @"\Logs\Log" + DateTime.Today.ToString("yyyyMMdd") + ".log";
- string DirPath = Application.StartupPath + @"\Logs";
-
- DirectoryInfo di = new DirectoryInfo(DirPath);
-
- foreach (DirectoryInfo dir in di.GetDirectories())
- {
- foreach (FileInfo file in dir.GetFiles())
- {
- if (file.Extension != ".log")
- {
- continue;
- }
- if (file.CreationTime < DateTime.Now.AddDays(-(keepDay)))
- {
- file.Delete();
- }
- }
- }
- di = null;
- }
- catch(Exception ex)
- {
- Log("파일 삭제 에러 발생 = " + ex);
- }
- }
- }
- }
|