123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Rms;
- using DllHsms;
- using DllHsmsWeb;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using ZipFileHelper;
- namespace DllEapDal
- {
- public class LogDal
- {
- private IDatabase CurrDb = null;
- public LogDal(IDatabase db)
- {
- CurrDb = db;
- }
- public List<string> ReadLog(Machine mac, DateTime starttime, DateTime endtime, ref string errorinfo)
- {
- try
- {
- string logfile = $"{mac.ID}_{mac.FCode}.log";//日志文件名称
- DateTime startdate = DateTime.Parse(starttime.ToString("yyyy-MM-dd"));
- DateTime enddate = DateTime.Parse(endtime.ToString("yyyy-MM-dd"));
- string logdir = AppConfigurtaionServices.Configuration["FileDir"];
- List<string> log = new List<string>();
- DirectoryInfo dirs = new DirectoryInfo(logdir);
- //获取事件范围目录
- List<DirectoryInfo> logdirs = dirs.GetDirectories().Where(t =>
- {
- DateTime currdate = DateTime.Parse($"{t.Name.Substring(3, 4)}-{t.Name.Substring(7, 2)}-{t.Name.Substring(9, 2)}");
- return currdate >= startdate && currdate <= enddate;
- }
- ).OrderBy(t => t.Name).ToList();
- //log.Add("测试1");
- //log.Add(logdirs.Count.ToString());
- if (logdirs.Count <= 0)
- return log;
- List<string> templist = null;
- //遍历目录文件,取对应的机台日志
- foreach (DirectoryInfo item in logdirs)
- {
- //log.Add(item.FullName);
- //log.Add(logfile);
- List<FileInfo> files = item.GetFiles().Where(t => t.Name == logfile).ToList();
- if (files.Count <= 0)
- continue;
- templist = ReadLog(files[0], starttime, endtime, ref errorinfo);
- if (templist == null)
- return null;
- log.AddRange(templist);
- }
- //log.Add("测试2");
- return log;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- private DateTime GetOperatorTime(string str)
- {
- string[] strs = str.Split(':');
- strs = strs[1].Split(' ');
- return DateTime.Parse(strs[0] + " " + strs[1]);
- }
- /// <summary>
- /// 从指定的文件中按时间范围取日志
- /// </summary>
- /// <param name="file"></param>
- /// <param name="starttime"></param>
- /// <param name="endtime"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public List<string> ReadLog(FileInfo file, DateTime starttime, DateTime endtime, ref string errorinfo)
- {
- try
- {
- //将文件读取到链表
- StreamReader sr = new StreamReader(file.FullName, Encoding.UTF8);
- List<string> files = new List<string>();
- string tempstr = sr.ReadLine();
- bool isstart = false;
- DateTime operatortime = DateTime.Now;
- while (tempstr != null)
- {
- //还没开始且不是时间标记,则直接读下行
- if (!isstart && !tempstr.Contains("发生时间"))
- {
- tempstr = sr.ReadLine();
- continue;
- }
- //还没开始且是时间标记,则要判断时间是否大于开始时间,如果小于开始时间则直接读下行
- if (!isstart && tempstr.Contains("发生时间"))
- {
- operatortime = GetOperatorTime(tempstr);
- if (operatortime > endtime)//操作时间大于结束时间,则直接结束
- break;
- if (operatortime >= starttime)
- {
- isstart = true;
- files.Add(tempstr);
- }
- tempstr = sr.ReadLine();
- continue;
- }
- if (isstart && !tempstr.Contains("发生时间"))
- {
- files.Add(tempstr);
- tempstr = sr.ReadLine();
- continue;
- }
- if (isstart && tempstr.Contains("发生时间"))
- {
- operatortime = GetOperatorTime(tempstr);
- if (operatortime > endtime)//操作时间大于结束时间,则直接结束
- break;
- files.Add(tempstr);
- tempstr = sr.ReadLine();
- continue;
- }
- }
- sr.Close();
- return files;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public List<string> ReadBusinessLog(Machine mac, DateTime starttime, DateTime endtime, ref string errorinfo)
- {
- try
- {
- string logfile = $"{mac.FCode}.log";//日志文件名称
- DateTime startdate = DateTime.Parse(starttime.ToString("yyyy-MM-dd"));
- DateTime enddate = DateTime.Parse(endtime.ToString("yyyy-MM-dd"));
- string logdir = AppConfigurtaionServices.Configuration["BusinessLogDir"];
- List<string> log = new List<string>();
- DirectoryInfo dirs = new DirectoryInfo(logdir);
- //获取事件范围目录
- List<DirectoryInfo> logdirs = dirs.GetDirectories().Where(t =>
- {
- DateTime currdate = DateTime.Parse($"{t.Name.Substring(3, 4)}-{t.Name.Substring(7, 2)}-{t.Name.Substring(9, 2)}");
- return currdate >= startdate && currdate <= enddate;
- }
- ).OrderBy(t => t.Name).ToList();
- if (logdirs.Count <= 0)
- return log;
- List<string> templist = null;
- //遍历目录文件,取对应的机台日志
- foreach (DirectoryInfo item in logdirs)
- {
- List<FileInfo> files = item.GetFiles().Where(t => t.Name == logfile).ToList();
- if (files.Count <= 0)
- continue;
- templist = ReadLog(files[0], starttime, endtime, ref errorinfo);
- if (templist == null)
- return null;
- log.AddRange(templist);
- }
- return log;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 获取列表
- /// </summary>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <param name="filter"></param>
- /// <param name="sort"></param>
- /// <param name="order"></param>
- /// <returns></returns>
- public IEnumerable<Log> GetLogList(int start, int length, string filter, string sort, string order, ref string errorinfo)
- {
- var list = CurrDb.FindListForCondition<Log>($" {filter} order by {sort} {order} limit {start - 1},{length}",
- ref errorinfo);
- return list;
- }
- /// <summary>
- /// 获取邮件配置总的记录数
- /// </summary>
- /// <param name="filter"></param>
- /// <returns></returns>
- public int GetLogCount(string filter)
- {
- string sql = new Log().GetSelectSql();
- sql = $"select count(1) from ({sql} where 1=1 {filter}) t";
- return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
- }
- public Log Get(int id)
- {
- return CurrDb.FindEntityFor<Log>(id);
- }
- public int Insert(Log log, string userCode)
- {
- return CurrDb.InsertFor(log, userCode);
- }
- public List<string> ReadLogList(string maccode, string starttime, string endtime, ref string errorinfo)
- {
- try
- {
- //string macode = "DA00012";
- //string starttime = "2020-11-11 02:50:04.629";
- //string endtime = "2020-11-11 03:50:04.629";
- int spinday = Convert.ToDateTime(endtime).Subtract(Convert.ToDateTime(starttime)).Duration().Days;
- string url = AppConfigurtaionServices.Configuration["FileDir"] + @"\";// @"D:\log\";
- var model = CurrDb.FindListForCondition<Machine>($" AND a.FCode='{maccode}'", ref errorinfo).FirstOrDefault();
- if (model == null)
- {
- errorinfo = "未找到机台号!!";
- return null;
- }
- int macid = model.ID;
- DateTime st = Convert.ToDateTime(starttime);
- List<string> vs = new List<string>();//文件url
- List<string> logs = new List<string>();//log内容
- string urlall = string.Empty;
- for (int i = 0; i <= spinday; i++)
- {
- urlall = url + st.ToString("yyyy-MM-dd") + @"\" + maccode + "_" + macid.ToString() + ".log";
- vs.Add(urlall);
- st = st.AddDays(1);
- }
- foreach (var item in vs)
- {
- if (System.IO.File.Exists(item))
- {
- string str1 = File.ReadAllText(item);
- string[] ss = str1.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);//3614
- 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));
- foreach (string item1 in ll)
- {
- //Console.WriteLine(item1);
- logs.Add(item1.Replace("<", "<").Replace(">", ">")
- .Replace("\r\n", "<br>").Replace(" ", " "));
- }
- }
- }
- return logs;
- }
- catch (IOException e)
- {
- return null;
- }
- }
- }
- }
|