Đọc từ Event Log (ASP.NET)
.NET Framework có một số lớp dùng để đọc và viết vào event log. Tất cả được lưu trong
System.Diagnostics namespace. Sau đây chúng tôi xin trình bày một đoạn code ASP.NET đơn giản để
hiển thị các mục lỗi trong event log trong được lưu giữ trong System Log.
<%@ Import Namespace=''System.Diagnostics'' %>
<%@ Import Namespace=''System.Drawing'' %>
<script language=''VB'' runat=''server''>
Sub Page_Load(source as Object, e as EventArgs)
If Not Page.IsPostBack Then
DisplayEventLog(''System'')
End If
End Sub
Sub btnSubmit_OnClick(source as Object, e as EventArgs)
DisplayEventLog(lstLog.SelectedItem.Value)
End Sub
Sub btnClear_OnClick(source as Object, e as EventArgs)
Dim objEventLog as New EventLog(lstLog.SelectedItem.Value)
objEventLog.Clear()
End Sub
Sub DisplayEventLog(strLogName as String)
Dim objRow as New TableRow
Dim objCell as New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''Type''
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''Date''
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''Time''
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''Source''
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''User''
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.BackColor = Color.Bisque
objCell.HorizontalAlign = HorizontalAlign.Center
objCell.Text = ''Computer''
objRow.Cells.Add(objCell)
tblLog.Rows.Add(objRow)
Dim objEventLog as EventLog = New EventLog(strLogName)
Dim objEntry as EventLogEntry
For Each objEntry in objEventLog.Entries
objRow = New TableRow
objCell = New TableCell
If objEntry.EntryType = EventLogEntryType.Error Then
objCell.BackColor = Color.Red
objCell.ForeColor = Color.White
objCell.Text = ''Error''
ElseIf objEntry.EntryType = EventLogEntryType.Information Then
objCell.Text = ''Information''
ElseIf objEntry.EntryType = EventLogEntryType.Warning Then
objCell.BackColor = Color.Yellow
objCell.Text = ''Warning''
ElseIf objEntry.EntryType = EventLogEntryType.SuccessAudit Then
objCell.Text = ''Success Audit''
ElseIf objEntry.EntryType = EventLogEntryType.FailureAudit Then
objCell.ForeColor = Color.Red
objCell.Text = ''Failure Audit''
End If
objCell.HorizontalAlign = HorizontalAlign.Center
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.TimeGenerated.ToShortDateString()
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.TimeGenerated.ToLongTimeString()
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.Source
objRow.Cells.Add(objCell)
objCell = New TableCell
If objEntry.UserName <> Nothing then
objCell.Text = objEntry.UserName
Else
objCell.Text = ''N/A''
End If
objRow.Cells.Add(objCell)
objCell = New TableCell
objCell.Text = objEntry.MachineName
objRow.Cells.Add(objCell)
tblLog.Rows.Add(objRow)
Next
End Sub
</script>
<html>
<body>
<form runat=''server''>
<h1>Event Log Viewer</h1>
<asp:listbox runat=''server'' id=''lstLog'' Rows=''1''>
<asp:listitem>Application</asp:listitem>
<asp:listitem>Security</asp:listitem>
<asp:listitem Selected=''True''>System</asp:listitem>
</asp:listbox>
<asp:button runat=''server'' id=''btnSubmit'' Text=''Display Event Log''
OnClick=''btnSubmit_OnClick'' />
<hr>
<asp:table runat=''server'' id=''tblLog'' CellPadding=''5''
CellSpacing=''0'' GridLines=''Both'' Font-Size=''10pt''
Font-Name=''Verdana'' />
<hr>
<asp:button runat=''server'' id=''btnClear'' Text=''Clear Event Log''
OnClick=''btnClear_OnClick'' />
</form>
</body>
</html>