| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Windows.Forms;using System.Text.RegularExpressions;namespace FPER{    class LogFileCreate    {        string Log_ONOFF = _Ini.Read_Ini("PROGRAM", "Log_ONOFF", 1024, Environment.CurrentDirectory + "\\Config.ini");        string Log_Cnt_Temp = _Ini.Read_Ini("PROGRAM", "Log_Cnt", 1024, Environment.CurrentDirectory + "\\Config.ini");        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)        {            if (Log_ONOFF == "ON")            {                int Log_Cnt = 30;                if (Log_Cnt_Temp != "")                {                    if (CheckNumber(Log_Cnt_Temp))                        Log_Cnt = Convert.ToInt32(Log_Cnt_Temp);                }                string FilePath = Application.StartupPath + @"\Logs\FPER\Log" + DateTime.Today.ToString("yyyyMMdd") + ".log";                string DirtopPath = Application.StartupPath + @"\Logs";                string DirbottomPath = Application.StartupPath + @"\Logs\FPER";                string temp;                DirectoryInfo ditop = new DirectoryInfo(DirtopPath);                DirectoryInfo dibottom = new DirectoryInfo(DirbottomPath);                FileInfo fi = new FileInfo(FilePath);                if (ditop.Exists != true) //top로그 폴더 없을때, 로그 폴더 생성                {                    Directory.CreateDirectory(DirtopPath);                }                if (dibottom.Exists != true) //bottom로그 폴더 없을때, 로그 폴더 생성                {                    Directory.CreateDirectory(DirbottomPath);                }                if (fi.Exists != true) //로그 파일 없을때, 로그 파일 생성                {                    using (StreamWriter sw = new StreamWriter(FilePath))                    {                        DeleteServiceLogByDay(Log_Cnt);   //현재일로부터 30일 이전 로그 파일 삭제                        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);                        if (str.Substring(0, 2) == "타입")                        {                            if (str.Substring(10, 2) == "응답")                            {                                sw.WriteLine(temp);                            }                            else                            {                                sw.WriteLine("");                                sw.WriteLine(temp);                            }                        }                        else                        {                            sw.WriteLine(temp);                        }                        sw.Close();                        sw.Dispose();                    }                }            }        }        internal void DeleteServiceLogByDay(int keepDay)        {            try            {                string DirPath = Application.StartupPath + @"\Logs\FPER";                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);            }        }        public static bool CheckNumber(string letter)        {            bool IsCheck = true;            Regex numRegex = new Regex(@"[0-9]");            Boolean ismatch = numRegex.IsMatch(letter);            if (!ismatch)            {                IsCheck = false;            }            return IsCheck;        }    }}
 |