Log.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. default:
  118. strLogName = "计量终端_";
  119. break;
  120. }
  121. string m_szRunPath;
  122. m_szRunPath = System.Environment.CurrentDirectory;
  123. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  124. {
  125. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  126. }
  127. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  128. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  129. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  130. {
  131. Directory.CreateDirectory(strPathFile);
  132. }
  133. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\" + strLogName + strDate + ".log", true);
  134. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  135. tw.WriteLine(str);
  136. tw.WriteLine("\r\n");
  137. tw.Close();
  138. }
  139. catch (Exception ex)
  140. {
  141. }
  142. }
  143. public class LogTwArray
  144. {
  145. public string Name;
  146. public DateTime LastWriteTime;
  147. public TextWriter tw = null;
  148. public string LastText = "";
  149. }
  150. static Dictionary<string, List<LogTwArray>> ht_pre = new Dictionary<string, List<LogTwArray>>();
  151. private static DateTime _lastCleanTime = DateTime.Now;
  152. private static Mutex mtx = new Mutex();
  153. static string m_szRunPath = System.Environment.CurrentDirectory;
  154. /// <summary>
  155. /// 写入日志
  156. /// </summary>
  157. /// <param name="prefix">日志文件名前缀</param>
  158. /// <param name="str"></param>
  159. public void WriteLog(string prefix, string str)
  160. {
  161. string strDate = DateTime.Now.ToString("yyyyMMdd");
  162. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  163. List<LogTwArray> tws = null;
  164. LogTwArray lta = null;
  165. try
  166. {
  167. if (ht_pre.ContainsKey(prefix))
  168. {
  169. tws = ht_pre[prefix];
  170. }
  171. else
  172. {
  173. tws = new List<LogTwArray>();
  174. if (!Directory.Exists(strPathFile))
  175. {
  176. Directory.CreateDirectory(strPathFile);
  177. }
  178. ht_pre.Add(prefix, tws);
  179. }
  180. lta = tws.Find(x => x.Name.Equals(strDate));
  181. if (lta == null || !lta.Name.Equals(strDate))
  182. {
  183. lta = new LogTwArray();
  184. lta.Name = strDate;
  185. lta.tw = new StreamWriter(strPathFile + "\\" + prefix + "_" + strDate + ".log", true);
  186. tws.Add(lta);
  187. }
  188. if (lta.LastText.Equals(str)) return;
  189. lta.LastWriteTime = DateTime.Now;
  190. lta.tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  191. lta.tw.WriteLine(str);
  192. lta.tw.WriteLine("\r\n");
  193. lta.tw.Flush();
  194. lta.LastText = str;
  195. }
  196. catch { }
  197. if (mtx.WaitOne(0))
  198. {
  199. try
  200. {
  201. lock (ht_pre)
  202. {
  203. DateTime dt = DateTime.Now;
  204. if (TimeSpan.FromTicks(dt.Ticks - _lastCleanTime.Ticks).TotalMinutes > 15)
  205. {
  206. foreach (List<LogTwArray> llta in ht_pre.Values)
  207. {
  208. List<LogTwArray> llta2 = llta.FindAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  209. if (llta2 != null)
  210. {
  211. foreach (LogTwArray lt in llta2)
  212. {
  213. lt.tw.Close();
  214. }
  215. }
  216. llta.RemoveAll(x => TimeSpan.FromTicks(dt.Ticks - x.LastWriteTime.Ticks).TotalMinutes > 15);
  217. }
  218. }
  219. }
  220. }
  221. finally
  222. {
  223. mtx.ReleaseMutex();
  224. }
  225. }
  226. }
  227. }
  228. }