欢迎光临
我们一直在努力

显示系统日志(ASP+)-ASP教程,ASP应用

建站超值云服务器,限时71元/月

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 doesnt 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 were 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();

}

</script>

<body bgcolor="#ffffff">

<h3>system event log</h3>

<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"

/>

</form>

</body>

</html>

the datagrid control only contains formatting instructions, nothing else. the grid is filled via the

page_load event, which merely opens the event log and then assigns the entries as the datasource property

of the datagrid. with the call of databind the data is poured into the table – all of it.

the amount of data is not exactly small, as the eventlogentry class possesses many properties and we just

want to have a neat overview. the next section deals with implementing this restriction.

restricting fields in the datagrid

our goal is to display only certain fields. this time, before getting into the code, we take a quick look

at the output:

in principle, this output is very similar to the preceding example, the only difference being the number

of columns displayed. this restriction is being done in the datagrid tag itself (speccolsonly.aspx

contains the entire code):

<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">

<property name="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"/>

</property>

</asp:datagrid>

the first important step is to set the autogeneratecolumns attribute to false. this prevents the

automatical display of all properties. now we can specify which columns we want.

i am using here four bound columns (bound to the data source).the headertext is being displayed in the top

row and in datafield the property to be read for filling this column is given.

i kept this example with columns intentionally simple. there are many more column types and when you start

playing around with the formatting, there are no limits to the designers frenzy. more than enough

examples for this can be found in the quickstart tutorial.

paging in the datagrid

for finishing off, i want to use another feature of the datagrid which is an old acquaintance for db

programmers – paging. the advantage of the datagrid is that paging needs (almost) no code. this might look

like the following:

this time i have again taken the whole source code of paging.aspx into the article for reading through:

<% @page language="c#" %>

<% @assembly name="system.diagnostics" %>

<% @import namespace="system.diagnostics" %>

<script language="c#" runat="server">

void page_load(object sender, eventargs e)

{

bindgrid();

}

void loggrid_page(object sender, datagridpagechangedeventargs e)

{

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_page"

bordercolor="black"

borderwidth="1"

gridlines="both"

cellpadding="3"

cellspacing="0"

font-name="verdana"

font-size="8pt"

headerstyle-backcolor="#aaaadd"

autogeneratecolumns="false">

<property name="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"/>

</property>

</asp:datagrid>

</form>

</body>

</html>

the first changes are found in the datagrid control:

allowpaging="true"

pagesize="10"

pagerstyle-mode="numericpages"

pagerstyle-horizontalalign="right"

pagerstyle-nextpagetext="next"

pagerstyle-prevpagetext="prev"

onpageindexchanged="loggrid_page"

the two most important attributes are the first and the last one: allowpaging and onpageindexchanged. the

first enables paging, the second is the event method triggered upon change of the page. the remaining

attributes are of cosmetic nature.

as we are working with a collection instead of database supplied data in this example, i made it (almost

too) easy for myself: im just binding the data onto the grid again. for better performance – especially

with databases – the data also should be reloaded in snippets.

conclusion

the actual purpose of todays article was not so much to learn reading the event log, but rather to

demonstrate how versatile the uses of the datagrid are outside the main application field of database

programming. very much of the functionality can be used, however, editing of event log entries (read-only)

doesnt make sense and thus cannot be used.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 显示系统日志(ASP+)-ASP教程,ASP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章