C# & VB.NET2006. 2. 20. 14:41

그냥 서버에 접속해서 이벤트로그를 보면 되지 않느냐라고 생각하실 수도 있지만...
단일한 관리용 웹페이지에서 이벤트로그, DB의 로그 등을 모두 보고 싶어 할 수도 있다.
아래 코드는 C#으로 작성한 ASP.NET 페이지로, 주어진 이름의 이벤트로그의 데이터를 읽어와서 그리드에 바인딩시키는 코드이다.

  1. private void Page_Load(object sender, System.EventArgs e){
  2.   this.DataGrid1.DataSource = this.GetData("Comm.SQL");
  3.   this.DataGrid1.DataBind();
  4. }
  5. private DataSet GetData(string eventLogName){
  6.   System.Diagnostics.EventLog oLog = null;
  7.   System.Diagnostics.EventLogEntryCollection oEntryCollection = null;
  8.   System.Data.DataSet dsEventLog = null;
  9.   System.Data.DataTable dtEventLog = null;
  10.   try{
  11.        dsEventLog = new DataSet("EventLogDataList");
  12.        dtEventLog = new DataTable(eventLogName);
  13.        dtEventLog.Columns.Add("Type");
  14.        dtEventLog.Columns.Add("Day");
  15.        dtEventLog.Columns.Add("Time");
  16.        dtEventLog.Columns.Add("Source");
  17.        dtEventLog.Columns.Add("Category");
  18.        dtEventLog.Columns.Add("Event");
  19.        dtEventLog.Columns.Add("User");
  20.        dtEventLog.Columns.Add("Machine");
  21.        oLog = new System.Diagnostics.EventLog(eventLogName);
  22.        oEntryCollection = oLog.Entries;
  23.        for(int i=0;i<oEntryCollection.Count;i++){
  24.            string[] arrLogEntry = new string[8];
  25.            arrLogEntry[0] = oEntryCollection[i].EntryType.ToString();
  26.            arrLogEntry[1] = oEntryCollection[i].TimeGenerated.ToShortDateString();
  27.            arrLogEntry[2] = oEntryCollection[i].TimeGenerated.ToShortTimeString();
  28.            arrLogEntry[2] = "<a href='webform2.aspx?LogName=" + eventLogName + "&Index=" + i.ToString() + "'>" + arrLogEntry[2] + "</a>";
  29.            arrLogEntry[3] = oEntryCollection[i].Source;
  30.            arrLogEntry[4] = oEntryCollection[i].Category;
  31.            arrLogEntry[5] = oEntryCollection[i].EventID.ToString();
  32.            arrLogEntry[6] = oEntryCollection[i].UserName;
  33.            arrLogEntry[7] = oEntryCollection[i].MachineName;
  34.             dtEventLog.Rows.Add(arrLogEntry);
  35.      }
  36.      dsEventLog.Tables.Add(dtEventLog);
  37.   }
  38.   catch(Exception ex)
  39.   {
  40.        throw ex;
  41.   }
  42.   return dsEventLog;
  43. }


위 코드는 이벤트뷰어와 똑같은 칼럼 순서로 이벤트 리스트를 그리드에 뿌리게 된다.
2번째 칼럼은 원래는 시간 데이터인데, 거기에 링크를 거는 부분이 있다.
이벤트 뷰어에서도 더블 클릭을 하면 이벤트에 대한 자세한 사항을 나타내는 창이 뜨는데,
아래는 바로 그 링크에서 연결되는 Detail 화면을 뿌리기 위한 코드이다.

  1. protected System.Web.UI.HtmlControls.HtmlInputText txtDay;
  2. protected System.Web.UI.HtmlControls.HtmlInputText txtTime;
  3. protected System.Web.UI.HtmlControls.HtmlInputText txtType;
  4. protected System.Web.UI.HtmlControls.HtmlInputText txtSource;
  5. protected System.Web.UI.HtmlControls.HtmlInputText txtCategory;
  6. protected System.Web.UI.HtmlControls.HtmlInputText txtEvent;
  7. protected System.Web.UI.HtmlControls.HtmlInputText txtUser;
  8. protected System.Web.UI.HtmlControls.HtmlInputText txtMachine;
  9. protected System.Web.UI.HtmlControls.HtmlTextArea txtDescription;
  10. private void Page_Load(object sender, System.EventArgs e)
  11. {
  12.   try
  13.   {
  14.     string strLogName = Request["LogName"];
  15.     string strIndex = Request["Index"];
  16.     int nIndex = Convert.ToInt32(strIndex);
  17.     this.SetContent(strLogName, nIndex);
  18.   }
  19.   catch(Exception ex)
  20.   {
  21.     Response.Write(ex.ToString());
  22.   }
  23. }
  24. private void SetContent(string logName, int index)
  25. {
  26.   System.Diagnostics.EventLog oLog = null;
  27.   System.Diagnostics.EventLogEntry oEntry = null;
  28.   try
  29.   {
  30.     oLog = new System.Diagnostics.EventLog(logName);
  31.     oEntry = oLog.Entries[index];
  32.     this.txtDay.Value = oEntry.TimeGenerated.ToShortDateString();
  33.     this.txtTime.Value = oEntry.TimeGenerated.ToLongTimeString();
  34.     this.txtType.Value = oEntry.EntryType.ToString();
  35.     this.txtSource.Value = oEntry.Source;
  36.     this.txtCategory.Value = oEntry.Category;
  37.     this.txtEvent.Value = oEntry.EventID.ToString();
  38.     this.txtUser.Value = oEntry.UserName;
  39.     this.txtMachine.Value = oEntry.MachineName;
  40.     this.txtDescription.Value = oEntry.Message;
  41.   }
  42.   catch(Exception ex)
  43.   {
  44.     throw ex;
  45.   }
  46. }
 
Posted by kkongchi