12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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();
- sw.Dispose();
- }
- }
- else
- {
- using (StreamWriter sw = File.AppendText(FilePath))
- {
- temp = string.Format("[{0}] : {1}", GetDateTime(), str);
- sw.WriteLine(temp);
- sw.Close();
- sw.Dispose();
- }
- }
- }
- catch
- {
- }
- DeleteServiceLogByDay(10); //현재일로부터 7일 이전 로그 파일 삭제
- }
- 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)
- {
- Log("[Log.cs]파일 삭제 에러 발생 = " + ex);
- }
- }
- }
- }
|