LogManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Configuration;
  8. namespace parkMonitor.LOG
  9. {
  10. /// <summary>
  11. /// 日志管理
  12. /// </summary>
  13. public class LogManager
  14. {
  15. public static string logAddressStr = "LogAddress";
  16. public static string logAddress;
  17. DateTime time; //文件创建时间
  18. private string logFileExtName = "log"; //日志文件扩展名
  19. private Encoding logFileEncoding = Encoding.UTF8; //日志文件编码格式
  20. private string logFileName = string.Empty; //日志文件名
  21. private string logPath = ""; //日志文件路径
  22. private bool writeLogTime = true; //log文件是否写时间
  23. private bool writeStatus = false; //是否写入标志位
  24. private static object obj = new object();
  25. /// <summary>
  26. /// 配置文件初始化
  27. /// </summary>
  28. public static void Init()
  29. {
  30. try
  31. {
  32. logAddress = ConfigurationManager.AppSettings[logAddressStr];
  33. }
  34. catch
  35. {
  36. Console.WriteLine("配置文件有误");
  37. }
  38. }
  39. /// <summary>
  40. /// 日志文件路径
  41. /// </summary>
  42. public string CreateLogPath()
  43. {
  44. if (logPath == null || logPath == string.Empty || time.ToString("yyyy-MM-dd") != System.DateTime.Now.ToString("yyyy-MM-dd"))
  45. {
  46. try
  47. {
  48. time = System.DateTime.Now;
  49. logPath = System.IO.Path.Combine(logAddress, time.ToString("yyyy-MM-dd"));
  50. }
  51. catch
  52. {
  53. Console.WriteLine("路径合成失败");
  54. }
  55. }
  56. if (!logPath.EndsWith(@"\"))
  57. {
  58. logPath += @"\";
  59. }
  60. if (!Directory.Exists(logPath))
  61. {
  62. try
  63. {
  64. Directory.CreateDirectory(logPath);
  65. }
  66. catch
  67. {
  68. Console.WriteLine("创建文件路径失败");
  69. }
  70. }
  71. return logPath;
  72. }
  73. /// <summary>
  74. /// 写日志
  75. /// </summary>
  76. public void WriteLog(LogType logType, string logFile, string msg)
  77. {
  78. CreateLogPath();
  79. lock (obj)
  80. {
  81. try
  82. {
  83. //创建log文件
  84. logFileName = string.Format("{0}{1}.{2}", logPath, logType, this.logFileExtName);
  85. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  86. {
  87. //是否写时间
  88. if (logType == LogType.database)
  89. {
  90. writeLogTime = false;
  91. }
  92. else
  93. {
  94. writeLogTime = true;
  95. }
  96. ////sql类型
  97. //if (logType == LogType.DATABASE)
  98. //{
  99. // writeStatus = true;
  100. //}
  101. //else
  102. //{
  103. // writeStatus = false;
  104. //}
  105. if (writeLogTime)
  106. {
  107. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") + logFile + ":" + msg);
  108. }
  109. else
  110. {
  111. sw.WriteLine(logFile + ":" + msg);
  112. }
  113. }
  114. }
  115. catch { }
  116. }
  117. }
  118. /// <summary>
  119. /// log类型不定
  120. /// </summary>
  121. /// <param name="logType"></param>
  122. /// <param name="logFile"></param>
  123. /// <param name="msg"></param>
  124. public void WriteLog(LogType logType, LogFile logFile, string msg)
  125. {
  126. this.WriteLog(logType, logFile.ToString(), msg);
  127. }
  128. /// <summary>
  129. /// log类型为null
  130. /// </summary>
  131. /// <param name="logType"></param>
  132. /// <param name="msg"></param>
  133. public void WriteLog(LogType logType, string msg)
  134. {
  135. this.WriteLog(logType, string.Empty, msg);
  136. }
  137. /// <summary>
  138. /// 读日志文件
  139. /// </summary>
  140. /// <param name="sqlStatus"></param>
  141. /// <param name="sqlMsg"></param>
  142. public void ReadLog(out string sqlStatus, out string sqlMsg, out int count)
  143. {
  144. sqlStatus = null;
  145. sqlMsg = null;
  146. count = 0;
  147. string date = System.DateTime.Now.ToString("yyyy-MM-dd");
  148. string filePath = logAddress + "\\\\" + date + "\\\\" + "DATABASE.log";
  149. try
  150. {
  151. List<string> logLines = null;
  152. if (File.Exists(filePath))
  153. {
  154. logLines = new List<string>(File.ReadAllLines(filePath));
  155. }
  156. if (logLines != null)
  157. {
  158. count = logLines.Count;
  159. string logLine = logLines[0];
  160. sqlStatus = logLine.Substring(0, 1);
  161. sqlMsg = logLine.Substring(2, logLine.Length - 2);
  162. logLines.RemoveAt(0);
  163. File.WriteAllLines(filePath, logLines.ToArray());
  164. }
  165. }
  166. catch (Exception e) { Console.WriteLine(e.Message); }
  167. }
  168. }
  169. /// <summary>
  170. /// log类型
  171. /// </summary>
  172. public enum LogFile
  173. {
  174. LOG,
  175. RESET,
  176. ERROR,
  177. WARNING,
  178. INFO,
  179. ERROR_NUMBERPLATE
  180. }
  181. /// <summary>
  182. /// log文件类型
  183. /// </summary>
  184. public enum LogType
  185. {
  186. database,
  187. process
  188. }
  189. }