1
0

LogHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. namespace YOLODetectProcessLib
  5. {
  6. /// <summary>
  7. /// 日志类
  8. /// </summary>
  9. public class LogHelper
  10. {
  11. private static LogHelper logHelper = new LogHelper();
  12. private static DateTime _starTime;
  13. private static string _folder;
  14. private static volatile object _logLocker = new object();
  15. private static volatile object _errorLogLocker = new object();
  16. private static volatile object _warnLogLocker = new object();
  17. private static volatile object _infoLogLocker = new object();
  18. private static string _errorLogFileName = string.Empty;
  19. private static string _warnLogFileName = string.Empty;
  20. private static string _infoLogFileName = string.Empty;
  21. private LogHelper()
  22. {
  23. #if ENABLE_AI_LOGS
  24. _folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AILogs");
  25. if (!Directory.Exists(_folder))
  26. {
  27. Directory.CreateDirectory(_folder);
  28. }
  29. #endif
  30. }
  31. /// <summary>
  32. /// info
  33. /// </summary>
  34. /// <param name="msg"></param>
  35. /// <param name="stackTrace"></param>
  36. public static void InfoLog(params string[] msgs)
  37. {
  38. Log(EnumLogType.InfoLog, msgs);
  39. }
  40. /// <summary>
  41. /// warn
  42. /// </summary>
  43. /// <param name="msg"></param>
  44. /// <param name="stackTrace"></param>
  45. public static void WarnLog(string msg, string stackTrace)
  46. {
  47. Log(EnumLogType.WarnLog, msg, stackTrace);
  48. }
  49. /// <summary>
  50. /// error
  51. /// </summary>
  52. /// <param name="msg"></param>
  53. /// <param name="stackTrace"></param>
  54. public static void ErrorLog(params string[] msgs)
  55. {
  56. Log(EnumLogType.ErrorLog, msgs);
  57. }
  58. /// <summary>
  59. /// fatal
  60. /// </summary>
  61. /// <param name="msg"></param>
  62. /// <param name="stackTrace"></param>
  63. public static void FatalLog(string msg, string stackTrace)
  64. {
  65. Log(EnumLogType.FatalLog, msg, stackTrace);
  66. }
  67. /// <summary>
  68. /// 记录
  69. /// </summary>
  70. /// <param name="ltype"></param>
  71. /// <param name="logs"></param>
  72. private static void Log(EnumLogType ltype, params string[] logs)
  73. {
  74. #if ENABLE_AI_LOGS
  75. DateTime currentTime = DateTime.Now;
  76. if ((currentTime - _starTime).TotalDays >= 1)
  77. {
  78. lock (_errorLogLocker)
  79. {
  80. _errorLogFileName = _folder + "\\" + currentTime.ToString("yyyyMMdd") + "_error.log";
  81. }
  82. lock (_warnLogLocker)
  83. {
  84. _warnLogFileName = _folder + "\\" + currentTime.ToString("yyyyMMdd") + "_warn.log";
  85. }
  86. lock (_infoLogLocker)
  87. {
  88. _infoLogFileName = _folder + "\\" + currentTime.ToString("yyyyMMdd") + "_info.log";
  89. }
  90. _starTime = currentTime;
  91. }
  92. string msg = DateTime.Now.ToString("yyyyMMdd_hhmmss_fffff ") + string.Join("\n", logs);
  93. switch (ltype)
  94. {
  95. case EnumLogType.InfoLog:
  96. lock (_infoLogLocker)
  97. {
  98. WriteLog(_infoLogFileName, msg);
  99. }
  100. break;
  101. case EnumLogType.WarnLog:
  102. lock (_infoLogLocker)
  103. {
  104. WriteLog(_warnLogFileName, msg);
  105. }
  106. break;
  107. case EnumLogType.ErrorLog:
  108. lock (_infoLogLocker)
  109. {
  110. WriteLog(_errorLogFileName, msg);
  111. }
  112. break;
  113. case EnumLogType.FatalLog:
  114. lock (_infoLogLocker)
  115. {
  116. WriteLog(_errorLogFileName, msg);
  117. }
  118. break;
  119. }
  120. #endif
  121. }
  122. private static void WriteLog(string fileName, string msg)
  123. {
  124. StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default);
  125. sw.WriteLine(msg);
  126. sw.Flush();
  127. sw.Close();
  128. }
  129. }
  130. }