09cf67997b40d470e96d0dde87888873e8c4745b.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using System.Text.RegularExpressions;
  9. namespace FPER
  10. {
  11. class LogFileCreate
  12. {
  13. string Log_ONOFF = _Ini.Read_Ini("PROGRAM", "Log_ONOFF", 1024, Environment.CurrentDirectory + "\\Config.ini");
  14. string Log_Cnt_Temp = _Ini.Read_Ini("PROGRAM", "Log_Cnt", 1024, Environment.CurrentDirectory + "\\Config.ini");
  15. public string GetDateTime()
  16. {
  17. DateTime NowDate = DateTime.Now;
  18. return NowDate.ToString("yyyy-MM-dd HH:mm:ss") + "." + NowDate.Millisecond.ToString("000");
  19. }
  20. public void Log(string str)
  21. {
  22. if (Log_ONOFF == "ON")
  23. {
  24. int Log_Cnt = 30;
  25. if (Log_Cnt_Temp != "")
  26. {
  27. if (CheckNumber(Log_Cnt_Temp))
  28. Log_Cnt = Convert.ToInt32(Log_Cnt_Temp);
  29. }
  30. string FilePath = Application.StartupPath + @"\Logs\FPER\Log" + DateTime.Today.ToString("yyyyMMdd") + ".log";
  31. string DirtopPath = Application.StartupPath + @"\Logs";
  32. string DirbottomPath = Application.StartupPath + @"\Logs\FPER";
  33. string temp;
  34. DirectoryInfo ditop = new DirectoryInfo(DirtopPath);
  35. DirectoryInfo dibottom = new DirectoryInfo(DirbottomPath);
  36. FileInfo fi = new FileInfo(FilePath);
  37. if (ditop.Exists != true) //top로그 폴더 없을때, 로그 폴더 생성
  38. {
  39. Directory.CreateDirectory(DirtopPath);
  40. }
  41. if (dibottom.Exists != true) //bottom로그 폴더 없을때, 로그 폴더 생성
  42. {
  43. Directory.CreateDirectory(DirbottomPath);
  44. }
  45. if (fi.Exists != true) //로그 파일 없을때, 로그 파일 생성
  46. {
  47. using (StreamWriter sw = new StreamWriter(FilePath))
  48. {
  49. DeleteServiceLogByDay(Log_Cnt); //현재일로부터 30일 이전 로그 파일 삭제
  50. temp = string.Format("[{0}] - {1}", GetDateTime(), str);
  51. sw.WriteLine(temp);
  52. sw.Close();
  53. sw.Dispose();
  54. }
  55. }
  56. else
  57. {
  58. using (StreamWriter sw = File.AppendText(FilePath)) // 로그 파일 있으면, AppendText
  59. {
  60. temp = string.Format("[{0}] - {1}", GetDateTime(), str);
  61. if (str.Substring(0, 2) == "타입")
  62. {
  63. if (str.Substring(10, 2) == "응답")
  64. {
  65. sw.WriteLine(temp);
  66. }
  67. else
  68. {
  69. sw.WriteLine("");
  70. sw.WriteLine(temp);
  71. }
  72. }
  73. else
  74. {
  75. sw.WriteLine(temp);
  76. }
  77. sw.Close();
  78. sw.Dispose();
  79. }
  80. }
  81. }
  82. }
  83. internal void DeleteServiceLogByDay(int keepDay)
  84. {
  85. try
  86. {
  87. string DirPath = Application.StartupPath + @"\Logs\FPER";
  88. DirectoryInfo di = new DirectoryInfo(DirPath);
  89. foreach (FileInfo file in di.GetFiles())
  90. {
  91. if (file.Extension == ".log")
  92. {
  93. if (file.CreationTime < DateTime.Now.AddDays(-(keepDay)))
  94. {
  95. file.Delete();
  96. }
  97. }
  98. }
  99. di = null;
  100. }
  101. catch (Exception ex)
  102. {
  103. Log("[Log.cs] 로그 파일 삭제 에러 발생 : " + ex);
  104. }
  105. }
  106. public static bool CheckNumber(string letter)
  107. {
  108. bool IsCheck = true;
  109. Regex numRegex = new Regex(@"[0-9]");
  110. Boolean ismatch = numRegex.IsMatch(letter);
  111. if (!ismatch)
  112. {
  113. IsCheck = false;
  114. }
  115. return IsCheck;
  116. }
  117. }
  118. }