LogDal.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllEapEntity.Rms;
  5. using DllHsms;
  6. using DllHsmsWeb;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using ZipFileHelper;
  14. namespace DllEapDal
  15. {
  16. public class LogDal
  17. {
  18. private IDatabase CurrDb = null;
  19. public LogDal(IDatabase db)
  20. {
  21. CurrDb = db;
  22. }
  23. public List<string> ReadLog(Machine mac, DateTime starttime, DateTime endtime, ref string errorinfo)
  24. {
  25. try
  26. {
  27. string logfile = $"{mac.ID}_{mac.FCode}.log";//日志文件名称
  28. DateTime startdate = DateTime.Parse(starttime.ToString("yyyy-MM-dd"));
  29. DateTime enddate = DateTime.Parse(endtime.ToString("yyyy-MM-dd"));
  30. string logdir = AppConfigurtaionServices.Configuration["FileDir"];
  31. List<string> log = new List<string>();
  32. DirectoryInfo dirs = new DirectoryInfo(logdir);
  33. //获取事件范围目录
  34. List<DirectoryInfo> logdirs = dirs.GetDirectories().Where(t =>
  35. {
  36. DateTime currdate = DateTime.Parse($"{t.Name.Substring(3, 4)}-{t.Name.Substring(7, 2)}-{t.Name.Substring(9, 2)}");
  37. return currdate >= startdate && currdate <= enddate;
  38. }
  39. ).OrderBy(t => t.Name).ToList();
  40. //log.Add("测试1");
  41. //log.Add(logdirs.Count.ToString());
  42. if (logdirs.Count <= 0)
  43. return log;
  44. List<string> templist = null;
  45. //遍历目录文件,取对应的机台日志
  46. foreach (DirectoryInfo item in logdirs)
  47. {
  48. //log.Add(item.FullName);
  49. //log.Add(logfile);
  50. List<FileInfo> files = item.GetFiles().Where(t => t.Name == logfile).ToList();
  51. if (files.Count <= 0)
  52. continue;
  53. templist = ReadLog(files[0], starttime, endtime, ref errorinfo);
  54. if (templist == null)
  55. return null;
  56. log.AddRange(templist);
  57. }
  58. //log.Add("测试2");
  59. return log;
  60. }
  61. catch (Exception ex)
  62. {
  63. errorinfo = ex.Message.ToString();
  64. return null;
  65. }
  66. }
  67. private DateTime GetOperatorTime(string str)
  68. {
  69. string[] strs = str.Split(':');
  70. strs = strs[1].Split(' ');
  71. return DateTime.Parse(strs[0] + " " + strs[1]);
  72. }
  73. /// <summary>
  74. /// 从指定的文件中按时间范围取日志
  75. /// </summary>
  76. /// <param name="file"></param>
  77. /// <param name="starttime"></param>
  78. /// <param name="endtime"></param>
  79. /// <param name="errorinfo"></param>
  80. /// <returns></returns>
  81. public List<string> ReadLog(FileInfo file, DateTime starttime, DateTime endtime, ref string errorinfo)
  82. {
  83. try
  84. {
  85. //将文件读取到链表
  86. StreamReader sr = new StreamReader(file.FullName, Encoding.UTF8);
  87. List<string> files = new List<string>();
  88. string tempstr = sr.ReadLine();
  89. bool isstart = false;
  90. DateTime operatortime = DateTime.Now;
  91. while (tempstr != null)
  92. {
  93. //还没开始且不是时间标记,则直接读下行
  94. if (!isstart && !tempstr.Contains("发生时间"))
  95. {
  96. tempstr = sr.ReadLine();
  97. continue;
  98. }
  99. //还没开始且是时间标记,则要判断时间是否大于开始时间,如果小于开始时间则直接读下行
  100. if (!isstart && tempstr.Contains("发生时间"))
  101. {
  102. operatortime = GetOperatorTime(tempstr);
  103. if (operatortime > endtime)//操作时间大于结束时间,则直接结束
  104. break;
  105. if (operatortime >= starttime)
  106. {
  107. isstart = true;
  108. files.Add(tempstr);
  109. }
  110. tempstr = sr.ReadLine();
  111. continue;
  112. }
  113. if (isstart && !tempstr.Contains("发生时间"))
  114. {
  115. files.Add(tempstr);
  116. tempstr = sr.ReadLine();
  117. continue;
  118. }
  119. if (isstart && tempstr.Contains("发生时间"))
  120. {
  121. operatortime = GetOperatorTime(tempstr);
  122. if (operatortime > endtime)//操作时间大于结束时间,则直接结束
  123. break;
  124. files.Add(tempstr);
  125. tempstr = sr.ReadLine();
  126. continue;
  127. }
  128. }
  129. sr.Close();
  130. return files;
  131. }
  132. catch (Exception ex)
  133. {
  134. errorinfo = ex.Message.ToString();
  135. return null;
  136. }
  137. }
  138. public List<string> ReadBusinessLog(Machine mac, DateTime starttime, DateTime endtime, ref string errorinfo)
  139. {
  140. try
  141. {
  142. string logfile = $"{mac.FCode}.log";//日志文件名称
  143. DateTime startdate = DateTime.Parse(starttime.ToString("yyyy-MM-dd"));
  144. DateTime enddate = DateTime.Parse(endtime.ToString("yyyy-MM-dd"));
  145. string logdir = AppConfigurtaionServices.Configuration["BusinessLogDir"];
  146. List<string> log = new List<string>();
  147. DirectoryInfo dirs = new DirectoryInfo(logdir);
  148. //获取事件范围目录
  149. List<DirectoryInfo> logdirs = dirs.GetDirectories().Where(t =>
  150. {
  151. DateTime currdate = DateTime.Parse($"{t.Name.Substring(3, 4)}-{t.Name.Substring(7, 2)}-{t.Name.Substring(9, 2)}");
  152. return currdate >= startdate && currdate <= enddate;
  153. }
  154. ).OrderBy(t => t.Name).ToList();
  155. if (logdirs.Count <= 0)
  156. return log;
  157. List<string> templist = null;
  158. //遍历目录文件,取对应的机台日志
  159. foreach (DirectoryInfo item in logdirs)
  160. {
  161. List<FileInfo> files = item.GetFiles().Where(t => t.Name == logfile).ToList();
  162. if (files.Count <= 0)
  163. continue;
  164. templist = ReadLog(files[0], starttime, endtime, ref errorinfo);
  165. if (templist == null)
  166. return null;
  167. log.AddRange(templist);
  168. }
  169. return log;
  170. }
  171. catch (Exception ex)
  172. {
  173. errorinfo = ex.Message.ToString();
  174. return null;
  175. }
  176. }
  177. /// <summary>
  178. /// 获取列表
  179. /// </summary>
  180. /// <param name="start"></param>
  181. /// <param name="end"></param>
  182. /// <param name="filter"></param>
  183. /// <param name="sort"></param>
  184. /// <param name="order"></param>
  185. /// <returns></returns>
  186. public IEnumerable<Log> GetLogList(int start, int length, string filter, string sort, string order, ref string errorinfo)
  187. {
  188. var list = CurrDb.FindListForCondition<Log>($" {filter} order by {sort} {order} limit {start - 1},{length}",
  189. ref errorinfo);
  190. return list;
  191. }
  192. /// <summary>
  193. /// 获取邮件配置总的记录数
  194. /// </summary>
  195. /// <param name="filter"></param>
  196. /// <returns></returns>
  197. public int GetLogCount(string filter)
  198. {
  199. string sql = new Log().GetSelectSql();
  200. sql = $"select count(1) from ({sql} where 1=1 {filter}) t";
  201. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
  202. }
  203. public Log Get(int id)
  204. {
  205. return CurrDb.FindEntityFor<Log>(id);
  206. }
  207. public int Insert(Log log, string userCode)
  208. {
  209. return CurrDb.InsertFor(log, userCode);
  210. }
  211. public List<string> ReadLogList(string maccode, string starttime, string endtime, ref string errorinfo)
  212. {
  213. try
  214. {
  215. //string macode = "DA00012";
  216. //string starttime = "2020-11-11 02:50:04.629";
  217. //string endtime = "2020-11-11 03:50:04.629";
  218. int spinday = Convert.ToDateTime(endtime).Subtract(Convert.ToDateTime(starttime)).Duration().Days;
  219. string url = AppConfigurtaionServices.Configuration["FileDir"] + @"\";// @"D:\log\";
  220. var model = CurrDb.FindListForCondition<Machine>($" AND a.FCode='{maccode}'", ref errorinfo).FirstOrDefault();
  221. if (model == null)
  222. {
  223. errorinfo = "未找到机台号!!";
  224. return null;
  225. }
  226. int macid = model.ID;
  227. DateTime st = Convert.ToDateTime(starttime);
  228. List<string> vs = new List<string>();//文件url
  229. List<string> logs = new List<string>();//log内容
  230. string urlall = string.Empty;
  231. for (int i = 0; i <= spinday; i++)
  232. {
  233. urlall = url + st.ToString("yyyy-MM-dd") + @"\" + maccode + "_" + macid.ToString() + ".log";
  234. vs.Add(urlall);
  235. st = st.AddDays(1);
  236. }
  237. foreach (var item in vs)
  238. {
  239. if (System.IO.File.Exists(item))
  240. {
  241. string str1 = File.ReadAllText(item);
  242. string[] ss = str1.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);//3614
  243. var ll = ss.Where(l => Convert.ToDateTime(l.Split(':')[1].Substring(0, 19)) > Convert.ToDateTime(starttime) && Convert.ToDateTime(l.Split(':')[1].Substring(0, 19)) < Convert.ToDateTime(endtime));
  244. foreach (string item1 in ll)
  245. {
  246. //Console.WriteLine(item1);
  247. logs.Add(item1.Replace("<", "&lt;").Replace(">", "&gt;")
  248. .Replace("\r\n", "<br>").Replace(" ", "&nbsp;"));
  249. }
  250. }
  251. }
  252. return logs;
  253. }
  254. catch (IOException e)
  255. {
  256. return null;
  257. }
  258. }
  259. }
  260. }