在 Windows 2000 (或 NT) 的事件日&;#35468;&;#23565;管理者&;#20358;&;#35498;可&;#35498;是最重要的&;#35338;息&;#20358;源,因&;#28858;所有的&;#30332;生的事件都&;#26371;&;#32000;&;#37636;在那&;#35041; ─ &;#24478;成功到&;#22196;重性的失&;#25943;。由於是如此的重要,那&;#40636;要是能&;#22816;透&;#36942; web &;#20358;使用,可不是更能突&;#39023;&;#21966;?
大家&;#25033;&;#35442;&;#23565;事件&;#27298;&;#35222;器不&;#26371;陌生,在&;#36889;篇文章我&;#23559;&;#35498;明如何使用 ASP.NET 和 .NET Framework SDK 能&;#22816;完美地仿效列出日&;#35468;。&;#28858;了&;#35731;&;#35712;者作&;#28858;&;#32244;&;#32722;,我先保留&;#23565;於&;#32178;&;#38913;呈&;#29694;&;#35443;&;#32048;&;#32000;&;#37636;&;#32048;&;#31680;的建置。
使用&;#36889;篇文章的原始&;#30908;在你的 Webserver 上必&;#38920;安&;#35037;
Microsoft .NET Framework SDK。 同&;#26178;我也假&;#35373;&;#35712;者&;#23565; C# 程式有一定程度的&;#35469;&;#35672;。
暴力手段法
&;#28858;了能更迅速且又不是那&;#40636;的乾&;#28136;俐落手段,我&;#20497;可以好好利用&;#36942;去&;#23565; ASP 的知&;#35672;&;#20358;&;#29986;生一系列事件。(即使是 table,&;#38614;然&;#36889;&;#20491;&;#31684;例&;#20006;不是要做 table)。程式的名&;#31281;也就是&;#36889;&;#20491;玩意的名&;#31281;:
simple.aspx. <% @Page Language="C#" %>
<% @Import Namespace="System.Diagnostics" %>
<%
EventLog aLog = new EventLog();
aLog.Log = "System";
aLog.MachineName = "."; // Local machine
string strImage = ""; // Icon for the 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 "\">&;amp;nbsp;|&;amp;nbsp;");
Response.Write(entry.TimeGenerated.ToString() "&;amp;nbsp;|&;amp;nbsp;");
Response.Write(entry.Source "&;amp;nbsp;|&;amp;nbsp;");
Response.Write(entry.EventID.ToString() "<br>\r\n");
}
%>
事件日&;#35468;的&;#39006;&;#21029;可以在&;#36889;&;#20491;名&;#31281;空&;#38291;找到
System.Diagnostics,必&;#38920;&;#23559;它放在&;#32178;&;#38913;&;#38283;始的地方。打&;#38283;日&;#35468;本身就是很直接: &;#29986;生新的
EventLog 物件, 指明
Log 和
MachineName ("." 是本地端的&;#27231;器)。然後我&;#20497;就&;#28310;&;#20633;&;#35712;取事件日&;#35468;。
我&;#20497;使用
foreach &;#36852;圈&;#20358;完成&;#36889;&;#20491;工作。&;#28858;使呈&;#29694;不&;#26371;那&;#40636;的缺乏&;#21109;意,我在每一&;#20491;&;#32000;&;#37636;&;#37666;都放一&;#20491;正&;#30906;的&;#22294;案,列出的&;#32000;&;#37636;&;#33287;&;#27298;&;#35222;器一般的&;#38918;序相反: &;#36942;去的&;#32000;&;#37636;&;#26371;列&;#28858;最&;#20778;先。
使用 DataGrid 更完美
在 ASP.NET 中有&;#35377;多的&;#21109;新,特&;#21029;是在&;#36039;料的展示,而且更棒的是&;#36039;料&;#20006;不一定要&;#20358;自&;#36039;料&;#24235;中。&;#23565;
DataGrid Web Control 也是如此,也就如其名&;#31281;一&;#27171;,&;#24478;&;#36039;料中&;#29986;生 table (grid)。唯一需求&;#35222;&;#36039;料&;#20358;源必&;#38920;是支援
ICollection 介面 ─ 也就是使用
EventLog 的集合
Entries。
以下的原始&;#30908; (
speccolsonly.aspx) &;#35498;明了使用 DataGrid 是如此的&;#31777;&;#21934;: <% @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();
}
</script>
DataGrid 控制&;#38917; (接下&;#20358;的程式) 只包含格式化的指令,&;#27794;有其他。Grid 是藉由
Page_Load 事件&;#20358;填&;#35036;&;#36914;去,就&;#36889;&;#27171;打&;#38283;了事件日&;#35468;,然後分配
Entries (&;#32000;&;#37636;) 作&;#28858; DataGrid 的
DataSource &;#23660;性。&;#38568;著呼叫
DataBind &;#36039;料就&;#28263;&;#36914;了 table 中 ─ 但是我&;#20497;只用到&;#27396;位,如下&;#35079;&;#35069;&;#22294;像所示:
&;#36889;&;#27171;的限制完成工作都是在 DataGrid 本身的&;#27161;&;#31844; (
speccolsonly.aspx 包含完整程式&;#30908;): <form runat="server">
<asp:DataGrid id="LogGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn HeaderText="TOF" DataField="EntryType" />
<asp:BoundColumn HeaderText="Date/Time" DataField="TimeGenerated"/>
<asp:BoundColumn HeaderText="Source" DataField="Source"/>
<asp:BoundColumn HeaderText="Event ID" DataField="EventID"/>
</Columns>
</asp:DataGrid>
</form>
首要的步&;#39519;就是&;#35373;定
AutoGenerateColumns &;#23660;性&;#28858; false,&;#36889;&;#27171;一&;#20358;可避免自&;#21205;&;#39023;示所有&;#23660;性。&;#29694;在我&;#20497;藉可以指明我&;#20497;所要的&;#27396;位。
我使用了四&;#20491;&;#32363;&;#32080;&;#27396;位 (&;#32363;&;#32080;到&;#36039;料&;#20358;源),
HeaderText &;#26371;呈&;#29694;在最上一列,而在
DataField 中&;#26371;&;#35712;取&;#23660;性&;#20358;填入所&;#32102;予的&;#27396;位中。
&;#31684;例中我故意&;#23559;&;#27396;位的&;#35373;定用得&;#31777;&;#21934;一&;#40670;。&;#36996;有&;#35377;多的&;#27396;位型&;#24907;,而&;#30070;你&;#38283;始&;#26371;使用格式化&;#20358;把玩&;#27396;位&;#26178;,&;#23565;&;#35373;&;#35336;者&;#20358;&;#35498;倒是可以&;#35731;你 "&;#30219;狂似的" 好好玩一玩呢!你可以在 QuickStart tutorial 找到更多的&;#31684;例。
DataGrid 中使用分&;#38913;
&;#28858;了完成工作,我使用另一&;#20491; DataGrid 特色,DB 程式&;#35373;&;#35336;&;#24107;&;#25033;&;#35442;都很熟&;#35672; ─ 分&;#38913;。DataGrid 的好&;#34389;就是分&;#38913;&;#24190;乎不&;#26371;用到任何的程式&;#30908;,看起&;#20358;就像&;#36889;&;#27171;:
&;#36889;一次我&;#23559;整&;#20491;原始&;#30908;
paging.aspx 放&;#36914;文章中方便&;#38321;&;#35712;: <% @Page Language="C#" %>
<% @Import Namespace="System.Diagnostics" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
BindGrid();
}
void LogGrid_Change(Object sender, DataGridPageChangedEventArgs e)
{
// Set CurrentPageIndex to the page the user clicked.
LogGrid.CurrentPageIndex = e.NewPageIndex;
// Rebind the data.
BindGrid();
}
void BindGrid()
{
EventLog aLog = new EventLog();
aLog.Log = "System";
aLog.MachineName = ".";
LogGrid.DataSource = aLog.Entries;
LogGrid.DataBind();
}
</script>
<body bgcolor="#ffffff">
<h3>System Event Log</h3>
<form runat="server">
<asp:DataGrid id="LogGrid" runat="server"
AllowPaging="True"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev"
OnPageIndexChanged="LogGrid_Change"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn HeaderText="TOF" DataField="EntryType" />
<asp:BoundColumn HeaderText="Date/Time" DataField="TimeGenerated"/>
<asp:BoundColumn HeaderText="Source" DataField="Source"/>
<asp:BoundColumn HeaderText="Event ID" DataField="EventID"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
首要的改&;#35722;可在 DataGrid 控制&;#38917;上找到: AllowPaging="True"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev"
OnPageIndexChanged="LogGrid_Change"
二&;#20491;最重要的&;#23660;性分&;#21029;在第一列和最後一列:
AllowPaging &;#33287;
OnPageIndexChanged。 第一列代表分&;#38913;,最後一列代表&;#30070;更&;#25563;到另一&;#38913;&;#26178;&;#26371;&;#35320;&;#30332;事件方法。其他的&;#23660;性都只是&;#35037;&;#39166;性&;#36074;。
由於&;#36889;&;#20491;&;#31684;例我&;#20497;所使用的是集合而不是&;#36039;料&;#24235;提供的&;#36039;料,我使用得很&;#31777;&;#21934;: 我只是&;#23559;&;#36039;料&;#32363;&;#32080;至 grid 中。&;#28858;了更好的&;#22519;行效能 ─ 特&;#21029;是&;#23565;&;#36039;料&;#24235;&;#20358;&;#35498; ─ 在&;#36889;&;#20491; "小程式" 中&;#36039;料&;#26371;被重新&;#36617;入。
&;#32080;&;#35542;
今天文章真正的目的&;#20006;不是完全在於事件日&;#35468;,而是在&;#35498;明 DataGrid 的使用非常多&;#27171;化,而不是&;#20677;止於&;#25033;用程式上&;#36039;料&;#24235;程式&;#35373;&;#35336;上的&;#27396;位而已。有&;#35377;多的功能可以使用,然而,要&;#32232;&;#36655;事件日&;#35468;&;#32000;&;#37636; (唯&;#35712;) 就&;#27794;有什&;#40636;意&;#32681;,&;#30070;然也就不能使用了&;#36889;&;#20491;功能了。
转自:动态网制作指南
www.knowsky.com