Log.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. try
  59. {
  60. using (StreamWriter sw = File.AppendText(FilePath)) // 로그 파일 있으면, AppendText
  61. {
  62. temp = string.Format("[{0}] - {1}", GetDateTime(), str);
  63. if (str.Substring(0, 2) == "타입")
  64. {
  65. if (str.Substring(10, 2) == "응답")
  66. {
  67. sw.WriteLine(temp);
  68. }
  69. else
  70. {
  71. sw.WriteLine("");
  72. sw.WriteLine(temp);
  73. }
  74. }
  75. else
  76. {
  77. sw.WriteLine(temp);
  78. }
  79. sw.Close();
  80. sw.Dispose();
  81. }
  82. }
  83. catch (FileNotFoundException e)
  84. {
  85. Console.WriteLine($"The file was not found: '{e}'");
  86. }
  87. catch (DirectoryNotFoundException e)
  88. {
  89. Console.WriteLine($"The directory was not found: '{e}'");
  90. }
  91. catch (IOException e)
  92. {
  93. Console.WriteLine($"The file could not be opened: '{e}'");
  94. }
  95. }
  96. }
  97. }
  98. internal void DeleteServiceLogByDay(int keepDay)
  99. {
  100. try
  101. {
  102. string DirPath = Application.StartupPath + @"\Logs\FPER";
  103. DirectoryInfo di = new DirectoryInfo(DirPath);
  104. foreach (FileInfo file in di.GetFiles())
  105. {
  106. if (file.Extension == ".log")
  107. {
  108. if (file.CreationTime < DateTime.Now.AddDays(-(keepDay)))
  109. {
  110. file.Delete();
  111. }
  112. }
  113. }
  114. di = null;
  115. }
  116. catch (Exception ex)
  117. {
  118. Log("[Log.cs] 로그 파일 삭제 에러 발생 : " + ex);
  119. }
  120. }
  121. public static bool CheckNumber(string letter)
  122. {
  123. bool IsCheck = true;
  124. Regex numRegex = new Regex(@"[0-9]");
  125. Boolean ismatch = numRegex.IsMatch(letter);
  126. if (!ismatch)
  127. {
  128. IsCheck = false;
  129. }
  130. return IsCheck;
  131. }
  132. }
  133. }