private void loadDataToGrid(List <TreeNode> nodes) { this.Cursor = Cursors.WaitCursor; try { LogDataLoadInfo loadInfo = new LogDataLoadInfo(); List <string> logFileNames = new List <string>(); foreach (TreeNode node in nodes) { if (node == null || node.Parent == null || node.Tag == null) { continue; } string fullFileName = _Current_LogFile_Path + @"\" + node.Tag.ToString() + ".txt"; logFileNames.Add(fullFileName); } if (logFileNames.Count == 0) { this.Cursor = Cursors.Default; return; } LogFileHelper.LoadDataToCtl(gridControl1, logFileNames.ToArray(), loadInfo); labExecuteTime.Text = "1)本次服务器记录时间从:" + loadInfo.BeginDate.ToString() + " 到:" + loadInfo.EndDate.ToString(); labLoadInfo.Text = "2)发现错误的消息有:" + loadInfo.ErrCount + " 个,超过1秒执行的方法有:" + loadInfo.OverTimeCount.ToString() + "个。"; } catch (Exception ex) { MessageBox.Show("加载日记有误,请检查是否有该目录的访问权限?" + ex.Message); } this.Cursor = Cursors.Default; }
private static decimal getExecuteTime(string strLine, LogDataLoadInfo loadInfo) { var s = System.Text.RegularExpressions.Regex.Replace(strLine, @"[^\w().]", ""); int firstFlag = s.IndexOf(COST_TEXT); if (firstFlag < 0) { return(0m); } int leave_length = firstFlag + COST_TEXT.Length; try { string val = s.Substring(leave_length, s.Length - leave_length); decimal dval = System.Decimal.Parse(val); if (dval > 1000m) { loadInfo.OverTimeCount++; } return(dval); } catch (Exception ex) { return(-1m); } }
/// <summary> /// 加载日志文件内容到DataGrid 网格控件中。 /// </summary> /// <param name="dGrid"></param> /// <param name="fullFileName"></param> public static void LoadDataToCtl(DevExpress.XtraGrid.GridControl dGrid, string[] fullFileName, LogDataLoadInfo loadInfo) { var gridView = dGrid.DefaultView as DevExpress.XtraGrid.Views.Grid.GridView; DataTable dtData = createFormLogFile(fullFileName, loadInfo); if (dtData == null) { return; } dGrid.DataSource = dtData.DefaultView; if (dtData == null) { return; } var col = gridView.Columns["CreateDate"]; col.Caption = "日期"; col.Width = 120; formatColumn(col); col = gridView.Columns["CreateTime"]; col.Caption = "时间"; col.Width = 60; formatColumn(col); col = gridView.Columns["RowIndex"]; col.Caption = "索引"; col.Width = 60; formatColumn(col); col = gridView.Columns["ClientIP"]; col.Caption = "用户机器IP"; col.Width = 100; formatColumn(col); col = gridView.Columns["MessageType"]; col.Caption = "消息类型"; col.Width = 100; formatColumn(col); col = gridView.Columns["Detail"]; col.Caption = "明细"; col.Width = 600; formatColumn(col); col = gridView.Columns["ExecuteTime"]; col.Caption = "花费(毫秒)"; col.Width = 100; formatColumn(col); }
//填充日志文件。 private static void fillDataTable(DataTable dtData, string fileName, LogDataLoadInfo loadInfo) { if (!System.IO.File.Exists(fileName)) { return; } System.IO.StreamReader sr = new System.IO.StreamReader(fileName); string line = null; DataRow curRow = null; bool hasSetBeginDate = false; while ((line = sr.ReadLine()) != null) { DateTime sDate = getDatetime(line); if (sDate == System.DateTime.MinValue) { if (curRow != null) { string strLine = curRow["Detail"].ToString() + line; curRow["Detail"] = strLine; curRow["ExecuteTime"] = getExecuteTime(strLine, loadInfo); } continue; } if (!hasSetBeginDate) { loadInfo.BeginDate = sDate; hasSetBeginDate = true; } loadInfo.EndDate = sDate; curRow = dtData.NewRow(); curRow["RowIndex"] = dtData.Rows.Count; curRow["CreateDate"] = sDate; if (sDate != System.DateTime.MinValue) { curRow["CreateTime"] = sDate.ToLongTimeString(); } curRow["ClientIP"] = getClientIP(line); curRow["MessageType"] = getMessageType(line, loadInfo); curRow["Detail"] = getDetail(line); curRow["ExecuteTime"] = getExecuteTime(line, loadInfo); dtData.Rows.Add(curRow); } _LasLoadLogData = line; sr.Close(); sr.Dispose(); }
//增加剩余的日志数据 private static void addLeaveLogData(DataTable dtData, string fileName, LogDataLoadInfo loadInfo) { System.IO.StreamReader sr = new System.IO.StreamReader(fileName); string line = null; bool hasFound = false; DataRow curRow = null; while ((line = sr.ReadLine()) != null) { if (!hasFound) { if (string.Compare(line, _LasLoadLogData, true) != 0) { continue; } hasFound = true; } DateTime sDate = getDatetime(line); if (sDate == System.DateTime.MinValue) { if (curRow != null) { curRow["Detail"] = curRow["Detail"].ToString() + line; } continue; } curRow = dtData.NewRow(); curRow["RowIndex"] = dtData.Rows.Count; curRow["CreateDate"] = sDate; if (sDate != System.DateTime.MinValue) { curRow["CreateTime"] = sDate.ToLongTimeString(); } curRow["ClientIP"] = getClientIP(line); curRow["MessageType"] = getMessageType(line, loadInfo); curRow["Detail"] = getDetail(line); curRow["ExecuteTime"] = getExecuteTime(line, loadInfo); dtData.Rows.Add(curRow); } _LasLoadLogData = line; sr.Close(); sr.Dispose(); }
//获取消息类型 public static string getMessageType(string strLine, LogDataLoadInfo loadInfo) { if (string.IsNullOrEmpty(strLine)) { return(string.Empty); } int firstFlag = strLine.IndexOf(')'); int lastFlag = strLine.IndexOf("-->"); if (firstFlag < 0 || lastFlag < 0) { return(string.Empty); } string str = strLine.Substring(firstFlag + 1, lastFlag - firstFlag - 1); if (string.Compare(str, "代码执行轨迹", true) != 0) { loadInfo.ErrCount++; } return(str); }
/// <summary> /// 那未增加的数据增加到网格中。 /// </summary> /// <param name="dGrid"></param> /// <param name="fullFileName"></param> public static void AddLeaveLogData(DevExpress.XtraGrid.GridControl dGrid, string[] fullFileName, LogDataLoadInfo loadInfo) { if (dGrid.DataSource == null) { return; } DataView dv = dGrid.DataSource as DataView; foreach (string fName in fullFileName) { addLeaveLogData(dv.Table, fName, loadInfo); } //dGrid.Refresh(); }
// 从日志文件中创建对应的数据表。 private static System.Data.DataTable createFormLogFile(string[] fullFileName, LogDataLoadInfo loadInfo) { DataTable dtLog = createDataTable(); SortedList <string, string> files = new SortedList <string, string>(); foreach (var f in fullFileName) { files.Add(f, f); } foreach (string fname in files.Values) { fillDataTable(dtLog, fname, loadInfo); } return(dtLog); }