Jonathan Goodyear, MCSD, MCP, CLS
我正式宣布今天为“免费代码星期五“。今天我将向大家免费提供一个用ASP .NET实现的工具,它可以用来查看并清除Windows2000的事件日志。我把这个工具的源代码附在了文章的后面。在这之前,我将解释一下它的工作原理。你也可以下载这段源代码(包括VB和C#两个版本)。
当这页第一次被载入时,我们将创建并显示一个ASP .NET DropDownList服务器控件,它列出了本地服务器上所有的事件日志。我们首先通过调用EventLog类中的GetEventLogs方法得到所有的事件日志,再选择出属于本地服务器的事件日志,这样我们就得到了所需要的信息。当然,我们也可以指定网络上的另一台计算机,同样能得到它所用的事件日志。GetEventLogs方法将返回一个EventLog对象数组,这个数组已被我们绑定到了DropDownList服务器控件。
如果你从DropDownList控件中选择一个事件日志并点击“Get Log Entries“按钮,将触发getMessages_Click事件。这个事件调用我们定义的GetLogEntries函数,得到所指定的事件日志中所有事件的入口,并据此建立起EventLogEntry对象集。接着,这个对象集被绑定到服务器Repeater控件, 后者用一张HTML格式的表格显示每一个EventLogEntry对象的属性。最后,这个函数调用EventLog对象的Close方法结束这个事件。
还有一个“Clear Log Entries“按钮,它将触发clearLog_Click事件。在这个事件中,我们首先将连接到指定的事件日志,再调用EventLog对象的Clear方法,接着调用Close方法。最后,我们调用GetLogEntries函数来刷新服务器Repeater控件。请看屏幕截图
事件察看器的源代码 |下载源代码(包括VB和C#两个版本)
== event_viewer_cs.aspx ==
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EventLog[] elArray = EventLog.GetEventLogs(".");
logs.DataSource = elArray;
logs.DataTextField = "Log";
logs.DataValueField = "Log";
logs.DataBind();
}
}
protected void getMessages_Click(object sender, EventArgs e)
{
GetLogEntries();
}
protected void clearLog_Click(object sender, EventArgs e)
{
EventLog el = new EventLog();
el.MachineName = ".";
el.Log = logs.SelectedItem.ToString();
el.Clear();
el.Close();
GetLogEntries();
}
protected void GetLogEntries()
{
EventLog el = new EventLog();
el.MachineName = ".";
el.Log = logs.SelectedItem.ToString();
messages.DataSource = el.Entries;
messages.DataBind();
el.Close();
}
protected string GetEventTypeDesc(EventLogEntryType elet)
{
switch(elet)
{
case EventLogEntryType.Error:
return "Error";
break;
case EventLogEntryType.Warning:
return "Warning";
break;
case EventLogEntryType.Information:
return "Information";
break;
case EventLogEntryType.SuccessAudit:
return "Success Audit";
break;
default: //EventLogEntryType.FailureAudit
return "Failure Audit";
break;
}
}
</script>
<html>
<head>
<title>Event Log Viewer</title>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="logs" runat="server" />
<asp:button id="getMessages" text="Get Log Entries"
οnclick="getMessages_Click" runat="server" />
<asp:button id="clearLog" text="Clear Log Entries"
οnclick="clearLog_Click" runat="server" />
</form>
<asp:repeater id="messages" runat="server">
<HeaderTemplate>
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<th>Type</th>
<th>Date/Time</th>
<th>Source</th>
<th>Category</th>
<th>Event</th>
<th>User</th>
<th>Computer</th>
<th>Message</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#GetEventTypeDesc(
((EventLogEntry)Container.DataItem).EntryType)%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).TimeGenerated%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).Source%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).Category%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).EventID%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).UserName%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).MachineName%>
</td>
<td>
<%#((EventLogEntry)Container.DataItem).Message%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</body>
</html>
== event_viewer_vb.aspx ==
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Diagnostics" %>
<script language="VB" runat="server">
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim elArray() As EventLog = EventLog.GetEventLogs(".")
With logs
.DataSource = elArray
.DataTextField = "Log"
.DataValueField = "Log"
.DataBind()
End With
End If
End Sub
Protected Sub getMessages_Click(sender As Object, e As EventArgs)
GetLogEntries()
End Sub
Protected Sub clearLog_Click(sender As Object, e As EventArgs)
Dim el As EventLog = New EventLog()
el.MachineName = "."
el.Log = logs.SelectedItem.ToString()
el.Clear()
el.Close()
GetLogEntries()
End Sub
Protected Sub GetLogEntries()
Dim el As EventLog = New EventLog()
With el
.MachineName = "."
.Log = logs.SelectedItem.ToString()
End With
messages.DataSource = el.Entries
messages.DataBind()
End Sub
Protected Function GetEventTypeDesc(elet As EventLogEntryType) _
As String
Select Case elet
Case EventLogEntryType.Error
return "Error"
Case EventLogEntryType.Warning
return "Warning"
Case EventLogEntryType.Information
return "Information"
Case EventLogEntryType.SuccessAudit
return "Success Audit"
Case Else 'EventLogEntryType.FailureAudit
return "Failure Audit"
End Select
End Function
</script>
<html>
<head>
<title>Event Log Viewer</title>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="logs" runat="server" />
<asp:button id="getMessages" text="Get Log Entries"
οnclick="getMessages_Click" runat="server" />
<asp:button id="clearLog" text="Clear Log Entries"
οnclick="clearLog_Click" runat="server" />
</form>
<asp:repeater id="messages" runat="server">
<HeaderTemplate>
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<th>Type</th>
<th>Date/Time</th>
<th>Source</th>
<th>Category</th>
<th>Event</th>
<th>User</th>
<th>Computer</th>
<th>Message</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#GetEventTypeDesc(Container.DataItem.EntryType)%>
</td>
<td>
<%#Container.DataItem.TimeGenerated%>
</td>
<td>
<%#Container.DataItem.Source%>
</td>
<td>
<%#Container.DataItem.Category%>
</td>
<td>
<%#Container.DataItem.EventID%>
</td>
<td>
<%#Container.DataItem.UserName%>
</td>
<td>
<%#Container.DataItem.MachineName%>
</td>
<td>
<%#Container.DataItem.Message%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</body>
</html>
转载于:https://www.cnblogs.com/leonardleonard/archive/2004/10/30/1928640.html