private void BackgroundWorkerXmpp_DoWork(object sender, DoWorkEventArgs e) { try { List <ReceiveThreadContext> contexts = new List <ReceiveThreadContext>(); int totalLines = AnalyseUtils.GetTotalLines(new List <string>() { xmppFilePath }); int currentLines = 0; int currentPencent = 0; using (StreamReader sr = new StreamReader(xmppFilePath, Encoding.GetEncoding("gb2312"))) { while (!sr.EndOfStream) { string line = sr.ReadLine(); int firstIndex = line.IndexOf('['); if (firstIndex > 0) { string strLogTime = line.Substring(line.IndexOf('|') + 2, 23); DateTime logTime = DateTime.Now; try { logTime = DateTime.ParseExact(strLogTime, "yyyy-MM-dd HH:mm:ss fff", System.Globalization.CultureInfo.CurrentCulture); } catch (Exception ex) { continue; } string threadNum = line.Substring(firstIndex + 1, line.IndexOf(']') - line.IndexOf('[') - 1); if (line.IndexOf("读取流--开始") >= 0) { ReceiveThreadContext context = contexts.FindLast(x => x.ThreadNum == threadNum); if (context == null || context.EndTime.Year > 2000) { context = new ReceiveThreadContext(); context.ThreadNum = threadNum; context.StartTime = logTime; contexts.Add(context); } if (context.LastLines.Count > 1) { context.LastLines.RemoveAt(1); } ReceiveData receiveData = new ReceiveData(); receiveData.Content = line; receiveData.LogTime = logTime; context.LastLines.Add(receiveData); } if (line.IndexOf("断开") >= 0) { //Console.WriteLine(threadNum); ReceiveThreadContext context = contexts.FindLast(x => x.ThreadNum == threadNum); if (context != null && context.EndTime.Year < 2000) { context.EndTime = DateTime.Now; ReceiveData receiveData = new ReceiveData(); receiveData.Content = line; receiveData.LogTime = logTime; context.LastLines.Add(receiveData); } else { Console.WriteLine(line); } } if (line.IndexOf("接收到数据:<iq id") > 0 && line.IndexOf(" type=\"get") > 0) { string name = line.Substring(line.IndexOf("to=") + 4, line.IndexOf("\"", line.IndexOf("to=") + 4) - line.IndexOf("to=") - 4); ReceiveThreadContext context = contexts.FindLast(x => x.ThreadNum == threadNum); if (context != null && context.EndTime.Year < 2000) { context.Name = name; //ReceiveData receiveData = new ReceiveData(); //receiveData.Content = line; //receiveData.LogTime = logTime; //context.LastLines.Add(receiveData); } } } //更新进度 currentLines++; int pencent = (currentLines * 100) / totalLines; if (pencent > 99) { pencent = 99; } if (pencent != currentPencent) { backgroundWorkerXmpp.ReportProgress(pencent); } currentPencent = pencent; } } StringBuilder sb = new StringBuilder(); foreach (var context in contexts) { if (string.IsNullOrEmpty(filterText) || (context.Name != null && !context.Name.Contains(filterText))) { sb.Append(string.Format("======={0}=======\n", context.Name)); foreach (var data in context.LastLines) { sb.Append(data.Content.Substring(data.Content.IndexOf('|'))); sb.Append(Environment.NewLine); } sb.Append(Environment.NewLine); } } string result = sb.ToString(); e.Result = string.IsNullOrEmpty(result) ? "无结果" : result; } catch (Exception ex) { e.Result = "日志分析异常:" + ex.ToString(); } }
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { try { RecordContext context = new RecordContext(); context.OrderRecords = new List <JspayOrderRecord>(); context.ParkRecords = new List <ParkRecord>(); context.DeviceCache = new List <DeviceInfo>(); List <string> lastLines = new List <string>(); int totalLines = AnalyseUtils.GetTotalLines(filePathList); if (totalLines == 0) { throw new Exception("日志内容为空"); } int currentLines = 0; int currentPencent = 0; foreach (var filePath in filePathList) { using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312"))) { while (!sr.EndOfStream) { string line = sr.ReadLine(); AnalyseUtils.ParseRecord(context, line, lastLines); AnalyseUtils.ParseOrderRecord(context, line, lastLines); if (lastLines.Count > 100) { lastLines.RemoveAt(0); } lastLines.Add(line); //更新进度 currentLines++; int pencent = (currentLines * 100) / totalLines; if (pencent > 99) { pencent = 99; } if (pencent != currentPencent) { backgroundWorker.ReportProgress(pencent); } currentPencent = pencent; } } } AnalyseUtils.MergeParkRecordAndOrder(context); StringBuilder sb = new StringBuilder(); foreach (var parkRecord in context.ParkRecords) { if ((string.IsNullOrEmpty(plate) || AnalyseUtils.FuzzyCompare(parkRecord.CredentialNo, plate) || parkRecord.HistoryCredentialNo.Any(y => AnalyseUtils.FuzzyCompare(y, plate))) && (string.IsNullOrEmpty(deviceName) || parkRecord.DeviceName.Contains(deviceName)) ) { sb.Append("======================="); sb.Append(parkRecord.CredentialNo); sb.Append("======================="); sb.Append(Environment.NewLine); foreach (var logNode in parkRecord.LogNodes) { sb.Append(logNode.LogTime.ToString("yyyy-MM-dd HH:mm:ss,fff ")); sb.Append(logNode.Message); sb.Append(Environment.NewLine); } } } e.Result = sb.ToString(); } catch (Exception ex) { e.Result = "日志分析异常:" + ex.ToString(); } }