Log.cs 10 KB

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