?? eventlog_article.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 2</title>
</head>
<body>
<h1 align="center">Complete example using custom errors and write errors to the Event log</h1>
<p>Writing to the Windows 2000 Event Log is a powerful feature of the ASP.NET and
.NET framework. For those individuals who work in a large company and want
to make track application errors, writing to the event log is a must!</p>
<p>There wasn't any complete demos actually showing from A - Z how this was
setup for the novice/common developer like me. Most novice developers need
to be spoon fed how things are done the first time, once they see a simple
example, they'll understand how the process works! This example is my
reference how to make a complete application from A - Z setup and fail to
understand how it works.</p>
<table width="100%">
<tr><td> </td>
<td><a href="/allzips/eventlogdotnet.zip"><img src='http://aspfree.com/images/downloadcode.gif' alt='downloadcode.gif (1195 bytes)' border='0' width='131' height='28'></a>
</td>
</tr>
</table>
<hr align="center" width="80%">
<h1><b>Step 1</b></h1>
<p>First of all the steps I followed was to create a simple web called <b><font color="#FF0000">eventlog.</font></b>
I also created the application rootso the global.asax file would fire.<br>(<a href="/aspnet/createapproot.aspx">Click here if you don't know how to create an application root)</a></p>
<p><img border="0" src="http://aspfree.com/images/eventlog1.gif"></p>
<h1>Step 2</h1>
I opened the global config.web file and turned on custom errors. Path to this
is <b>c:\winnt\microsoft.net\framework\..</b> There are 3 choices available currently
<i>On, Off and RemoteOnly</i>. From attending the conference, the
recommended was <b>RemoteOnly</b>. This means anyone not on the console of
the machine will see a friendly error and not the real thing. For this
example I chose <b>On</b>. You also could leave the global config.web file
custom errors turned off and configure at application level's web.config Either
way works just fine.
<br><br>
<b>Config.web -- this file is placed in the root of the \eventlog application
root.</b>
<p><configuration> <br>
<system.web><br>
<customErrors mode="On" defaultRedirect="/eventlog/customerrorpage.aspx"><br>
<error statusCode="404" redirect="/eventlog/404Page.aspx"/><br>
<error statusCode="403" redirect="/eventlog/403page.aspx"/><br>
</customErrors> <br>
</system.web><br>
</configuration> </p>
<h1>Step 3</h1>
The next few items are just to create sample pages to make the application
complete. I created a <b>config.web, global.asax, Default.aspx page</b>,
and three sample error pages. <b>404page.aspx, 403page.aspx </b>and<b>
customerrorpage.aspx page.</b><br><br>
Here are those pages code for all pages.
<p><b><i>Global.asax Page - </i>This uses the Application_OnError event to capture
stuff if an error happens</b></p>
<p><%@ Import Namespace="System" %><br>
<%@ Import Namespace="System.Diagnostics" %><br>
<script language="VB" runat=server><br>
<br>
Public Sub Application_OnError(Sender as Object, E as EventArgs)<br>
<b>'Captures the error and converts to a string</b><br>
dim LastError as Exception = Server.GetLastError()<br>
Dim ErrMessage as String = LastError.toString()<br>
<br>
Dim LogName As String = "MyLog"<br>
Dim Message As String = "Url " & Request.Path & " Error: " & ErrMessage<br>
<br>
<b>' Create Event Log if It Doesn't Exist<br>
</b> If (Not EventLog.SourceExists(LogName)) Then<br>
EventLog.CreateEventSource(LogName, LogName)<br>
End if<br>
<br>
Dim Log as New EventLog<br>
Log.Source = LogName<br>
<br>
<b> 'These are the five options that will display a different icon.<br>
'The numbers are just to show the order. These aren't required<br>
</b> Log.WriteEntry(Message,
EventLogEntryType.Information, 1)<br>
' Log.WriteEntry(Message,
EventLogEntryType.Error, 2)<br>
' Log.WriteEntry(Message, EventLogEntryType.Warning, 3)<br>
' Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4)<br>
' Log.WriteEntry(Message,
EventLogEntryType.FailureAudit, 5)<br>
End Sub<br>
</script></p>
<p><b>Default.aspx page</b></p>
<p><html><br>
<head><br>
</head><br>
<body><br>
<form method="post" action="eventlog.aspx" name="form1" id="number"><br>
<br>
<asp:Button id="abutton" type="submit" text="Click Me to generate an error" runat="server" /><br>
</form><br>
</body><br>
</html></p>
<p><b>Eventlog.aspx page -- This raises the Error!</b><br>
<br>
<% @Language="VB" %><br>
<script language="VB" runat=server><br>
Sub Page_Load(Sender As Object, E As EventArgs)<br>
<br>
<b> 'Declare all variables<br>
</b> dim x as integer<br>
dim y as integer<br>
dim z as integer<br>
<br>
<b>'set x and y to values to be divided by zero<br>
</b> x = 1<br>
y = 0<br>
<br>
<b> 'perform the division by zero to raise the error<br>
</b> z = x/y<br>
<br>
End Sub<br>
</script><br>
<html><br>
<head></head><br>
<body><br>
Stuff was added to the event log<br>
</body><br>
</html><br>
<b><br>
Customerrorpage.aspx</b>
</p>
<p><html><br>
<head></head><br>
<body><br>
<h1>custom error page</h1><br>
</body><br>
</html><br>
<br>
<b>404page.aspx --Capture all 404(Not Found pages)</b>
</p>
<p><html><br>
<head></head><br>
<body><br>
<h1>404 error page</h1><br>
</body><br>
</html><br>
<br>
<b>403page.aspx --Capture all 403(Restricted pages)</b>
</p>
<p><html><br>
<head></head><br>
<body><br>
<h1>403 error page</h1><br>
</body><br>
</html>
</p>
<h1>Step 4</h1>
After all webs are created, config.web files in place. It was time to test out the
application to see if it works. Type in http://localhost/eventlog/default.aspx file, this will display a
button. Click it and see if this
actually creates the log and writes the information to the event log.
Actually only the custom error page will be displayed, the eventlog.aspx page will error and be transfered
to the customerrorpage.aspx. The URL will be
something like this. <br><br>
<b>http://localhost/eventlog/customerrorpage.aspx?aspxerrorpath=/eventlog/eventlog.aspx</b>
<p>The page will appear like this</p>
<p><img border="0" src="http://aspfree.com/images/eventlog2.gif"></p>
<h1>Step 5. </h1>
Verify the log was created and entry was placed in that log. The
example shows a <font color="#FF0000"><b>Critical</b></font> error
message. You also can show <b><font color="#0000FF">informational</font></b>
or <font color="#000000"><b>yellow</b></font><font color="#FFFF00"> </font>warning
messages. For this example we showed all 5 possible entries<br><br>
<p><img border="0" src="http://aspfree.com/images/eventlog3.gif"><br>
Open the Error and view the message. If the customerror wasn't turned on, this error
would have shown up in the browser instead of the eventlog. Not pretty!!
<br><br>
<p><img border="0" src="http://aspfree.com/images/eventlog4.gif"></p>
<p>Thats it! This was a high-level example with examples but hopefully
helps in understanding how a sample application and using the new Error-handling
features of ASP.NET... Enjoy!!<br>
<hr width="75%">
<h4 class="dtH4">Parameters for WriteEntry Method</h4>
<dl>
<dt><i>source</i>
<dd>The source by which the application is registered on the specified
computer.
<dt><i>message</i>
<dd>The string to write to the event log.
<dt><i>type</i>
<dd>One of the EventLogEntryType values.
<dt><i>eventID</i>
<dd>The application-specific identifier for the event.
<dt><i>category</i>
<dd>The application-specific subcategory associated with the message.
<dt><i>rawData</i>
<dd>An array of bytes that holds the binary data associated with the entry.</dd>
</dl>
<p><b>Five possible types of Event log messages. These show the different
types of icons</b></p>
<table class="dtTABLE" cellSpacing="0">
<tbody>
<tr vAlign="top">
<th>Member Name</th>
<th>Description</th>
</tr>
<tr vAlign="top">
<td><b>Error</b></td>
<td>An error event. This indicates a significant problem the
user should know about; usually a loss of functionality or data.</td>
</tr>
<tr vAlign="top">
<td><b>FailureAudit</b></td>
<td>A failure audit event. This indicates a security event
that occurs when an audited access attempt fails; for example, a failed
attempt to open a file.</td>
</tr>
<tr vAlign="top">
<td><b>Information</b></td>
<td>An information event. This indicates a significant,
successful operation.</td>
</tr>
<tr vAlign="top">
<td><b>SuccessAudit</b></td>
<td>A success audit event. This indicates a security event
that occurs when an audited access attempt is successful; for example,
logging on successfully.</td>
</tr>
<tr vAlign="top">
<td><b>Warning</b></td>
<td>A warning event. This indicates a problem that is not
immediately significant, but that may signify conditions that could
cause future problems.</td>
</tr>
</tbody>
</table>
<p> </p>
<p>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -