- 爱易网页
-
ASP教程
- 显示系统日志(ASP+)
日期:2013-09-08 浏览次数:21051 次
Written by: Christoph Wille
Translated by: Bernhard Spuida
First published: 8/11/2000
Under Windows 2000 (or NT) the Event Log is about the most important source of information for the
administrator because all events that occurred are logged there - from success to catastrophical failure.
And as it is so important, what would be more obvious than to make it accessible via the web?
The Event Viewer should be familiar to nearly everyone (see picture). In this article I will demonstrate
how the list of entries can be emulated very elegantly using ASP.NET and the .NET Framework SDK. As an
exercise for the reader I will leave the construction of a page for the full details of an entry.
The use of the source code in this article requires the Microsoft .NET Framework SDK installed on a
Webserver. I also presume that the reader is familiar to some degree with the C# programming language.
The Brute Force Method
When we have to be quick and dirty, knowledge from the days of ASP can very well be used to generate a
list of events (Even with a table, though this example doesn't do that). The name of the program is the
name of the game: simple.aspx.
<% @Page Language="C#" %>
<% @Import Namespace="System.Diagnostics" %>
<%
EventLog aLog = new EventLog();
aLog.Log = "System";
aLog.MachineName = "."; // Lokale Maschine
string strImage = ""; // Icon für das Event
Response.Write("<p>There are " + aLog.Entries.Count +
" entries in the System event log.</p>");
foreach (EventLogEntry entry in aLog.Entries)
{
switch (entry.EntryType)
{
case EventLogEntryType.Warning:
strImage = "warning.png";
break;
case EventLogEntryType.Error:
strImage = "error.png";
break;
default:
strImage = "info.png";
break;
}
Response.Write("<img src=\"" + strImage + "\"> | ");
Response.Write(entry.TimeGenerated.ToString() + " | ");
Response.Write(entry.Source + " | ");
Response.Write(entry.EventID.ToString() + "
\r\n");
}
%>
The classes for the Event Log are found in the Namespace System.Diagnostics which is bound in at the
beginning of the page. Opening the log is in itself straightforward: create a new EventLog object, specify
the Log and the MachineName ("." is the local machine). And we're ready to read from the Event Log.
This is done in a foreach loop. To make the listing less unimaginative, I put the correct icon before each
entry. By the way, the listing of entries is the reverse of the usual order in the Event Viewer: here, the
oldest entries are listed first.
More elegant with the DataGrid
ASP.NET comes with many innovations, especially for displaying data. And the good part about that is that
the data does not always have to come out of a database. This also is true for the DataGrid Web Control
which, as the name says, creates a table (grid) out of the data. The only requirement is that the data
source supports the ICollection interface - and the Entries Collection of the EventLog does just that.
The following source code (datagrid.aspx) shows how simple using the DataGrid is:
<% @Page Language="C#" %>
<% @Import Namespace="System.Diagnostics" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
EventLog aLog = new EventLog();
aLog.Log = "System";
aLog.MachineName = ".";
LogGrid.DataSource = aLog.Entries;
LogGrid.DataBind();
}