小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#使用Log Parser 2.2 + MSchart打造簡易Windows日志分析工具

 llyyt85 2013-06-26

C#使用Log Parser 2.2 + MSchart打造簡易Windows日志分析工具

分類: .NET 797人閱讀 評論(3) 收藏 舉報

前段時間做了一個簡易的Windows日志分析工具(主要針對Windows系統(tǒng)日志SysEvent.Evt這個文件),主要是使用了Log Parser 2.2和MSchart控件,在此和大家分享一下!

首先是Log Parser 2.2,關(guān)于它的介紹我就不多說了,下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=24659

第一步是安裝Log Parser 2.2,然后新建一個C#窗體項目,在資源管理器中選中“引用”右鍵“添加引用”,選擇Log Parser 2.2安裝目錄下的“LogParser.dll”

 添加引用

然后確定返回主窗體頁面,添加一個button和一個dataGridView控件,在button的Click事件寫入如下代碼:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string sql = @"SELECT EventID, TimeGenerated, SourceName, Message FROM E:\SysEvent.Evt";//此為系統(tǒng)日志文件路徑  
  4.     DataTable dt = readFromEvt(sql);  
  5.     writeToDataBase(dt);  
  6.     dataGridView2.DataSource = dt;  
  7.     MessageBox.Show("讀取完畢!");  
  8. }  
  1. public DataTable readFromEvt(string sql)  
  2. {  
  3.     try  
  4.     {  
  5.         DataTable datat = new DataTable();  
  6.         datat.Columns.Add("事件ID", typeof(string));  
  7.         datat.Columns.Add("日期", typeof(string));  
  8.         datat.Columns.Add("來源", typeof(string));  
  9.         datat.Columns.Add("描述", typeof(string));   
  10.         // Instantiate the LogQuery object    
  11.         LogQuery oLogQuery = new LogQuery();  
  12.         // Instantiate the Event Log Input Format object    
  13.         EvtInputFormat oEvtInputFormat = new EvtInputFormat();  
  14.         // Set its "direction" parameter to "BW"    
  15.         oEvtInputFormat.direction = "BW";  
  16.         // Create the query    
  17.         string query = sql;  
  18.         // Execute the query    
  19.         LogRecordSet oRecordSet = oLogQuery.Execute(query, oEvtInputFormat);  
  20.         while (!oRecordSet.atEnd())  
  21.         {  
  22.             var itemData = oRecordSet.getRecord();  
  23.             DataRow dr = datat.NewRow();  
  24.             dr["事件ID"] = itemData.getValue("EventID").ToString();  
  25.             dr["日期"] = itemData.getValue("TimeGenerated").ToString();  
  26.             dr["來源"] = itemData.getValue("SourceName").ToString();  
  27.             dr["描述"] = itemData.getValue("Message").ToString();  
  28.             datat.Rows.Add(dr);  
  29.             oRecordSet.moveNext();  
  30.         }  
  31.           
  32.         // Close the recordset    
  33.         oRecordSet.close();  
  34.         return datat;  
  35.     }  
  36.     catch (System.Runtime.InteropServices.COMException exc)  
  37.     {  
  38.         MessageBox.Show("Unexpected error: " + exc.Message);  
  39.         return null;  
  40.     }   
  41. }  

至此已經(jīng)完成了SysEvent.Evt日志文件內(nèi)容的讀取,這里是讀取到一個DataTable并且綁定到dataGridView控件上,其中上面代碼中還有一個函數(shù)writeToDataBase(dt);主要是將這些數(shù)據(jù)寫到數(shù)據(jù)庫中。

接下來是MSchart,VS2010自帶這個控件,具體在工具箱數(shù)據(jù)欄下,名字是“Chart”,如果是VS2008的話還需要下載安裝一下,下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=14422,安裝好以后,在“工具箱”右鍵“選擇項”,“瀏覽”在Microsoft Chart Controls安裝目錄把Assemblies下的dll都添加進去(實際只用System.Windows.Forms.DataVisualization.dll),確定返回工具箱在數(shù)據(jù)欄下就可以看到“Chart”控件,拖拽一個到窗體上,然后再加入一個button,在button的Click事件寫入如下代碼:

  1. private void button3_Click(object sender, EventArgs e)  
  2.         {  
  3.             DB db = new DB();  
  4.             string sql = @"select top 5 EventID,count(*) as Num from EvevtLog group by EventID order by count(*) desc";  
  5.             DataTable dt = db.GetDataTable(sql);  
  6.             dataGridView2.DataSource = dt;  
  7.             // Set series chart type  
  8.             Chart1.Series[0].ChartType = SeriesChartType.Bar;  
  9.             // Show as 3D  
  10.             Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;  
  11.             // Draw as 3D Cylinder  
  12.             Chart1.Series[0]["DrawingStyle"] = "Cylinder";  
  13.             //Chart1.BackColor = Color.Azure; //圖表背景色  
  14.             Chart1.Titles.Add("事件"); //圖表標題  
  15.             //設(shè)置圖表的數(shù)據(jù)源              
  16.             Chart1.DataSource = dt;  
  17.             //設(shè)置圖表Y軸對應(yīng)項  
  18.             Chart1.Series[0].YValueMembers = "Num";  
  19.             //Chart1.Series[1].YValueMembers = "Volume2";  
  20.             //設(shè)置圖表X軸對應(yīng)項  
  21.             Chart1.Series[0].XValueMember = "EventID";  
  22.             Chart1.Series[0].IsValueShownAsLabel = true; //是否顯示數(shù)據(jù)  
  23.             Chart1.Series[0].IsVisibleInLegend = false; //是否顯示數(shù)據(jù)說明  
  24.             Chart1.Series[0].MarkerStyle = MarkerStyle.Circle; //線條上的數(shù)據(jù)點標志類型  
  25.             Chart1.Series[0].MarkerSize = 8; //標志大小  
  26.             Chart1.ChartAreas[0].AxisX.LineColor = Color.Blue; //X 軸顏色  
  27.             Chart1.ChartAreas[0].AxisY.LineColor = Color.Blue; //Y 軸顏色  
  28.             Chart1.ChartAreas[0].AxisX.LineWidth = 2; //X 軸寬度  
  29.             Chart1.ChartAreas[0].AxisY.LineWidth = 2; //Y 軸寬度  
  30.             Chart1.ChartAreas[0].AxisY.Title = "事件出現(xiàn)頻率"; //Y 軸標題  
  31.             //綁定數(shù)據(jù)  
  32.             Chart1.DataBind();   
  33.         }  

我這里面db.GetDataTable(sql);是從數(shù)據(jù)庫里抽取數(shù)據(jù)出來展示,各位其實可以按需選擇,關(guān)于展示部分,Chart控件很強大,我這里只做了一個統(tǒng)計展示的例子,其他的各位可以深入研究一下,最后看一下效果。

效果圖

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多