Log.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace Common
  9. {
  10. public class Log
  11. {
  12. /// <summary>
  13. /// 禁止通过new创建实例
  14. /// </summary>
  15. private Log() { }
  16. private static Log log;
  17. // 定义一个标识确保线程同步
  18. private static readonly object locker = new object();
  19. public static Log GetInstance()
  20. {
  21. if (log == null)
  22. {
  23. lock (locker)
  24. {
  25. if (log == null)
  26. {
  27. log = new Log();
  28. }
  29. }
  30. }
  31. return log;
  32. }
  33. /// <summary>
  34. /// 写入日志
  35. /// </summary>
  36. /// <param name="iType">0智能终端 1数据采集 2网络状态 3计量实绩 4计量监控 5远程计量 6静态衡 7动态衡 8成品秤</param>
  37. /// <param name="str"></param>
  38. public void WriteLog(int iType, string str)
  39. {
  40. try
  41. {
  42. string strLogName = "";
  43. switch (iType)
  44. {
  45. case 0:
  46. strLogName = "计量终端_";
  47. break;
  48. case 1:
  49. strLogName = "数据采集_";
  50. break;
  51. case 2:
  52. strLogName = "网络状态_";
  53. break;
  54. case 3:
  55. strLogName = "计量实绩_";
  56. break;
  57. case 4:
  58. strLogName = "计量监控_";
  59. break;
  60. case 5:
  61. strLogName = "远程计量_";
  62. break;
  63. case 6:
  64. strLogName = "静态衡计量_";
  65. break;
  66. case 7:
  67. strLogName = "动态衡计量_";
  68. break;
  69. case 8:
  70. strLogName = "热送磅计量_";
  71. break;
  72. case 9:
  73. strLogName = "提示信息_";
  74. break;
  75. case 10:
  76. strLogName = "打印日志_";
  77. break;
  78. case 11:
  79. strLogName = "静态衡公共事件_";
  80. break;
  81. case 12:
  82. strLogName = "主线程扫码设备_";
  83. break;
  84. case 13:
  85. strLogName = "tryCatch异常_";
  86. break;
  87. case 14:
  88. strLogName = "保存按钮状态_";
  89. break;
  90. case 15:
  91. strLogName = "按钮点击日志_";
  92. break;
  93. case 16:
  94. strLogName = "服务调用日志_";
  95. break;
  96. case 17:
  97. strLogName = "自动卸货日志_";
  98. break;
  99. case 18:
  100. strLogName = "复磅计量_";
  101. break;
  102. case 19:
  103. strLogName = "热送磅计量异常_";
  104. break;
  105. case 20:
  106. strLogName = "零点报警_";
  107. break;
  108. case 22:
  109. strLogName = "皮带秤计量异常_";
  110. break;
  111. case 23:
  112. strLogName = "吊钩秤计量异常_";
  113. break;
  114. case 24:
  115. strLogName = "检化验接口日志_";
  116. break;
  117. case 25:
  118. strLogName = "保存按钮延迟点击日志_";
  119. break;
  120. case 26:
  121. strLogName = "led推送日志_";
  122. break;
  123. case 27:
  124. strLogName = "frmOneYard_close日志_";
  125. break;
  126. case 28:
  127. strLogName = "上秤到下秤全语音跟踪";
  128. break;
  129. case 29:
  130. strLogName = "车号未注册_";
  131. break;
  132. case 30:
  133. strLogName = "车号注册接口_";
  134. break;
  135. case 31:
  136. strLogName = "frmOneYard_打开跟踪日志_";
  137. break;
  138. case 32:
  139. strLogName = "frmMain定时器重启日志_";
  140. break;
  141. default:
  142. strLogName = "计量终端_";
  143. break;
  144. }
  145. string m_szRunPath;
  146. m_szRunPath = System.Environment.CurrentDirectory;
  147. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  148. {
  149. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  150. }
  151. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  152. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  153. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  154. {
  155. Directory.CreateDirectory(strPathFile);
  156. }
  157. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\" + strLogName + strDate + ".log", true);
  158. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  159. tw.WriteLine(str);
  160. tw.WriteLine("\r\n");
  161. tw.Close();
  162. }
  163. catch (Exception ex)
  164. {
  165. }
  166. }
  167. public class LogTwArray
  168. {
  169. public string Name;
  170. public DateTime LastWriteTime;
  171. public TextWriter tw = null;
  172. public string LastText = "";
  173. }
  174. static Dictionary<string, List<LogTwArray>> ht_pre = new Dictionary<string, List<LogTwArray>>();
  175. private static DateTime _lastCleanTime = DateTime.Now;
  176. private static Mutex mtx = new Mutex();
  177. static string m_szRunPath = System.Environment.CurrentDirectory;
  178. /// <summary>
  179. /// 写入日志
  180. /// </summary>
  181. /// <param name="prefix">日志文件名前缀</param>
  182. /// <param name="str"></param>
  183. public void WriteLog(string prefix, string str)
  184. {
  185. string strDate = DateTime.Now.ToString("yyyyMMdd");
  186. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  187. List<LogTwArray> tws = null;
  188. LogTwArray lta = null;
  189. try
  190. {
  191. if (ht_pre.ContainsKey(prefix))
  192. {
  193. tws = ht_pre[prefix];
  194. }
  195. else
  196. {
  197. tws = new List<LogTwArray>();
  198. if (!Directory.Exists(strPathFile))
  199. {
  200. Directory.CreateDirectory(strPathFile);
  201. }
  202. ht_pre.Add(prefix, tws);
  203. }
  204. lta = tws.Find(x => x.Name.Equals(strDate));
  205. if (lta == null || !lta.Name.Equals(strDate))
  206. {
  207. lta = new LogTwArray();
  208. lta.Name = strDate;
  209. lta.tw = new StreamWriter(strPathFile + "\\" + prefix + "_" + strDate + ".log", true);
  210. tws.Add(lta);
  211. }
  212. if (lta.LastText.Equals(str)) return;
  213. lta.LastWriteTime = DateTime.Now;
  214. lta.tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  215. lta.tw.WriteLine(str);
  216. lta.tw.WriteLine("\r\n");
  217. lta.tw.Flush();
  218. lta.LastText = str;
  219. }
  220. catch { }
  221. if (mtx.WaitOne(0))
  222. {
  223. try
  224. {
  225. lock (ht_pre)
  226. {
  227. DateTime dt = DateTime.Now;
  228. if (TimeSpan.FromTicks(dt.Ticks - _lastCleanTime.Ticks).TotalMinutes > 15)
  229. {
  230. foreach (List<LogTwArray> llta in ht_pre.Values)
  231. {
  232. List<LogTwArray> llta2 = llta.FindAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  233. if (llta2 != null)
  234. {
  235. foreach (LogTwArray lt in llta2)
  236. {
  237. lt.tw.Close();
  238. }
  239. }
  240. llta.RemoveAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  241. }
  242. }
  243. }
  244. }
  245. finally
  246. {
  247. mtx.ReleaseMutex();
  248. }
  249. }
  250. }
  251. }
  252. }