LogManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Test
  6. {
  7. /// <summary>
  8. /// 日志管理
  9. /// </summary>
  10. public class LogManager
  11. {
  12. private string filepath = "log";
  13. //public static string logAddressStr = "filepath";
  14. //public static string logAddress;
  15. DateTime time; //文件创建时间
  16. private string logFileExtName = "log"; //日志文件扩展名
  17. private Encoding logFileEncoding = Encoding.UTF8; //日志文件编码格式
  18. private string logFileName = string.Empty; //日志文件名
  19. private string logPath = ""; //日志文件路径
  20. private bool writeLogTime = true; //log文件是否写时间
  21. private bool writeStatus = false; //是否写入标志位
  22. private static object obj = new object();
  23. /// <summary>
  24. /// 配置文件初始化
  25. /// </summary>
  26. public static void Init()
  27. {
  28. //try
  29. //{
  30. // logAddress = ConfigurationManager.AppSettings[logAddressStr];
  31. //}
  32. //catch
  33. //{
  34. // Console.WriteLine("配置文件有误");
  35. //}
  36. }
  37. /// <summary>
  38. /// 日志文件路径
  39. /// </summary>
  40. public string CreateLogPath()
  41. {
  42. if (!Directory.Exists(filepath))
  43. {
  44. try
  45. {
  46. Directory.CreateDirectory(filepath);
  47. }
  48. catch
  49. {
  50. Console.WriteLine("创建文件路径失败");
  51. }
  52. }
  53. if (logPath == null || logPath == string.Empty || time.ToString("yyyy-MM-dd") != System.DateTime.Now.ToString("yyyy-MM-dd"))
  54. {
  55. try
  56. {
  57. time = System.DateTime.Now;
  58. logPath = System.IO.Path.Combine(filepath, time.ToString("yyyy-MM-dd"));
  59. }
  60. catch
  61. {
  62. Console.WriteLine("路径合成失败");
  63. }
  64. }
  65. if (!logPath.EndsWith(@"\"))
  66. {
  67. logPath += @"\";
  68. }
  69. if (!Directory.Exists(logPath))
  70. {
  71. try
  72. {
  73. Directory.CreateDirectory(logPath);
  74. }
  75. catch
  76. {
  77. Console.WriteLine("创建文件路径失败");
  78. }
  79. }
  80. return logPath;
  81. }
  82. /// <summary>
  83. /// 写日志
  84. /// </summary>
  85. public void WriteLog(LogType logType, string logFile, string msg)
  86. {
  87. CreateLogPath();
  88. lock (obj)
  89. {
  90. try
  91. {
  92. //创建log文件
  93. logFileName = string.Format("{0}{1}.{2}", logPath, logType, this.logFileExtName);
  94. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  95. {
  96. //是否写时间
  97. if (logType == LogType.PROCESS)
  98. {
  99. writeLogTime = true;
  100. }
  101. else
  102. {
  103. writeLogTime = false;
  104. }
  105. ////sql类型
  106. //if (logType == LogType.DATABASE)
  107. //{
  108. // writeStatus = true;
  109. //}
  110. //else
  111. //{
  112. // writeStatus = false;
  113. //}
  114. if (writeLogTime)
  115. {
  116. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") + logFile + ":" + msg);
  117. }
  118. else
  119. {
  120. sw.WriteLine(logFile + "" + msg);
  121. }
  122. }
  123. }
  124. catch { }
  125. }
  126. }
  127. /// <summary>
  128. /// log类型不定
  129. /// </summary>
  130. /// <param name="logType"></param>
  131. /// <param name="logFile"></param>
  132. /// <param name="msg"></param>
  133. public void WriteLog(LogType logType, LogFile logFile, string msg)
  134. {
  135. this.WriteLog(logType, logFile.ToString(), msg);
  136. }
  137. /// <summary>
  138. /// log类型为null
  139. /// </summary>
  140. /// <param name="logType"></param>
  141. /// <param name="msg"></param>
  142. public void WriteLog(LogType logType, string msg)
  143. {
  144. this.WriteLog(logType, string.Empty, msg);
  145. }
  146. /// <summary>
  147. /// 读日志文件
  148. /// </summary>
  149. /// <param name="sqlStatus"></param>
  150. /// <param name="sqlMsg"></param>
  151. public List<string> ReadLog()
  152. {
  153. List<string> digitList = new List<string>();
  154. //string date = System.DateTime.Now.ToString("yyyy-MM-dd");
  155. //string filePath = logAddress + "\\\\" + date + "\\\\" + "CREDENCES.log";
  156. string filePath = filepath + "\\\\" + "CREDENCES.log";
  157. try
  158. {
  159. List<string> logLines = null;
  160. if (File.Exists(filePath))
  161. {
  162. logLines = new List<string>(File.ReadAllLines(filePath));
  163. File.Delete(filePath);
  164. }
  165. if (logLines != null)
  166. {
  167. for (int i = 0; i < 300; i++)
  168. {
  169. string logLine = logLines[i];
  170. digitList.Add(logLine);
  171. logLines.RemoveAt(i);
  172. }
  173. File.WriteAllLines(filePath, logLines.ToArray());
  174. }
  175. }
  176. catch (Exception e) { Console.WriteLine(e.Message); }
  177. return digitList;
  178. }
  179. public void WriteCredence(string msg)
  180. {
  181. lock (obj)
  182. {
  183. try
  184. {
  185. //创建log文件
  186. string logname = string.Format("{0}.{1}", "CREDENCES", this.logFileExtName);
  187. logFileName = filepath + "\\" + logname;
  188. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  189. {
  190. if (msg.Length == 8)
  191. {
  192. sw.WriteLine(msg);
  193. }
  194. else
  195. {
  196. sw.WriteLine(msg);
  197. }
  198. }
  199. }
  200. catch { }
  201. }
  202. }
  203. public void WriteData(string name, string msg)
  204. {
  205. CreateLogPath();
  206. lock (obj)
  207. {
  208. try
  209. {
  210. //创建log文件
  211. string logname = string.Format("{0}.{1}", name, "txt");
  212. //logFileName = filepath + "\\" + logname;
  213. logFileName = logPath + "\\" + logname;
  214. using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
  215. {
  216. if (msg.Length == 8)
  217. {
  218. sw.WriteLine(msg);
  219. }
  220. else
  221. {
  222. sw.WriteLine(msg);
  223. }
  224. }
  225. }
  226. catch { }
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// log类型
  232. /// </summary>
  233. public enum LogFile
  234. {
  235. LOG,
  236. RESET,
  237. ERROR,
  238. WARNING,
  239. INFO,
  240. ERROR_NUMBERPLATE
  241. }
  242. /// <summary>
  243. /// log文件类型
  244. /// </summary>
  245. public enum LogType
  246. {
  247. PROCESS,
  248. CREDENCES
  249. }
  250. }