Log.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. case 33:
  142. strLogName = "停留超时";
  143. break;
  144. case 34:
  145. strLogName = "重量保存_";
  146. break;
  147. case 35:
  148. strLogName = "硬盘录像机拍照_";
  149. break;
  150. case 99:
  151. strLogName = "双扫码";
  152. break;
  153. default:
  154. strLogName = "计量终端_";
  155. break;
  156. }
  157. string m_szRunPath;
  158. m_szRunPath = System.Environment.CurrentDirectory;
  159. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  160. {
  161. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  162. }
  163. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  164. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  165. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  166. {
  167. Directory.CreateDirectory(strPathFile);
  168. }
  169. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\" + strLogName + strDate + ".log", true);
  170. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  171. tw.WriteLine(str);
  172. tw.WriteLine("\r\n");
  173. tw.Close();
  174. }
  175. catch (Exception ex)
  176. {
  177. }
  178. }
  179. public class LogTwArray
  180. {
  181. public string Name;
  182. public DateTime LastWriteTime;
  183. public TextWriter tw = null;
  184. public string LastText = "";
  185. }
  186. static Dictionary<string, List<LogTwArray>> ht_pre = new Dictionary<string, List<LogTwArray>>();
  187. private static DateTime _lastCleanTime = DateTime.Now;
  188. private static Mutex mtx = new Mutex();
  189. static string m_szRunPath = System.Environment.CurrentDirectory;
  190. /// <summary>
  191. /// 写入日志
  192. /// </summary>
  193. /// <param name="prefix">日志文件名前缀</param>
  194. /// <param name="str"></param>
  195. public void WriteLog(string prefix, string str)
  196. {
  197. string strDate = DateTime.Now.ToString("yyyyMMdd");
  198. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  199. List<LogTwArray> tws = null;
  200. LogTwArray lta = null;
  201. try
  202. {
  203. if (ht_pre.ContainsKey(prefix))
  204. {
  205. tws = ht_pre[prefix];
  206. }
  207. else
  208. {
  209. tws = new List<LogTwArray>();
  210. if (!Directory.Exists(strPathFile))
  211. {
  212. Directory.CreateDirectory(strPathFile);
  213. }
  214. ht_pre.Add(prefix, tws);
  215. }
  216. lta = tws.Find(x => x.Name.Equals(strDate));
  217. if (lta == null || !lta.Name.Equals(strDate))
  218. {
  219. lta = new LogTwArray();
  220. lta.Name = strDate;
  221. lta.tw = new StreamWriter(strPathFile + "\\" + prefix + "_" + strDate + ".log", true);
  222. tws.Add(lta);
  223. }
  224. if (lta.LastText.Equals(str)) return;
  225. lta.LastWriteTime = DateTime.Now;
  226. lta.tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  227. lta.tw.WriteLine(str);
  228. lta.tw.WriteLine("\r\n");
  229. lta.tw.Flush();
  230. lta.LastText = str;
  231. }
  232. catch { }
  233. if (mtx.WaitOne(0))
  234. {
  235. try
  236. {
  237. lock (ht_pre)
  238. {
  239. DateTime dt = DateTime.Now;
  240. if (TimeSpan.FromTicks(dt.Ticks - _lastCleanTime.Ticks).TotalMinutes > 15)
  241. {
  242. foreach (List<LogTwArray> llta in ht_pre.Values)
  243. {
  244. List<LogTwArray> llta2 = llta.FindAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  245. if (llta2 != null)
  246. {
  247. foreach (LogTwArray lt in llta2)
  248. {
  249. lt.tw.Close();
  250. }
  251. }
  252. llta.RemoveAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  253. }
  254. }
  255. }
  256. }
  257. finally
  258. {
  259. mtx.ReleaseMutex();
  260. }
  261. }
  262. }
  263. }
  264. }