Log.cs 9.6 KB

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