Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 6th, 2004, 11:03 AM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 151 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 37 sec
Reputation Power: 10
Complete example using custom errors and write errors to the Event log

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!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! 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. [bold]Step 1[/bold] First of all the steps I followed was to create a simple web called eventlog. I also created the application root so the global.asax file would fire. Here is how to setup an application root: [bold]1.[/bold] Open Internet Service Manager MMC [italic](Start button, Control Panel, Administrative Tools, Internet Service Manager)[/italic] [bold]2.[/bold] Located on the left side of the MMC, locate the Default Web and click the + button to expand the list of webs. [bold]3.[/bold] Right-click on the web your going to make an application root and choose properties. [bold]4.[/bold] Click the Create button. [bold]5.[/bold] After your done, the folder picture will appear like a box that is open. The application root is now created. [bold]Step 2[/bold] I opened the global web.config file and turned on custom errors. Path to this is c:\winnt\microsoft.net\framework\.. There are 3 choices available currently On, Off and RemoteOnly. From attending the conference, the recommended was RemoteOnly. 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 On. You also could leave the global web.config file custom errors turned off and configure at application level's web.config Either way works just fine. [bold]web.config -- this file is placed in the root of the \eventlog application root.[/bold]

<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="/eventlog/customerrorpage.aspx">
<error statusCode="404" redirect="/eventlog/404Page.aspx"/>
<error statusCode="403" redirect="/eventlog/403page.aspx"/>
</customErrors>
</system.web>
</configuration>



[bold]Step 3[/bold] The next few items are just to create sample pages to make the application complete. I created a web.config, global.asax, Default.aspx page, and three sample error pages. 404page.aspx, 403page.aspx and customerrorpage.aspx page. Here are those pages code for all pages. [bold]Global.asax Page - This uses the Application_OnError event to capture stuff if an error happens[/bold]

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Diagnostics" %>
<script language="VB" runat=server>

Public Sub Application_OnError(Sender as Object, E as EventArgs)
'Captures the error and converts to a string
dim LastError as Exception = Server.GetLastError()
Dim ErrMessage as String = LastError.toString()

Dim LogName As String = "MyLog"
Dim Message As String = "Url " & Request.Path & " Error: " & ErrMessage

' Create Event Log if It Doesn't Exist
If (Not EventLog.SourceExists(LogName)) Then
EventLog.CreateEventSource(LogName, LogName)
End if

Dim Log as New EventLog
Log.Source = LogName

'These are the five options that will display a different icon.
'The numbers are just to show the order. These aren't required
Log.WriteEntry(Message, EventLogEntryType.Information, 1)
' Log.WriteEntry(Message, EventLogEntryType.Error, 2)
' Log.WriteEntry(Message, EventLogEntryType.Warning, 3)
' Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4)
' Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5)
End Sub
</script>



[bold]Default.aspx page[/bold]

<% @Language="VB" %>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
If IsPostBack Then
'Declare all variables
dim x as integer
dim y as integer
dim z as integer

'set x and y to values to be divided by zero
x = 1
y = 0

'perform the division by zero to raise the error
z = x/y
End Sub
</script>

<html>
<head>
</head>
<body>
<form method="post" action="eventlog.aspx" name="form1" id="number">

<asp:Button id="abutton" type="submit" text="Click Me to generate an error" runat="server" />
</form>
</body>
</html>



[bold]Customerrorpage.aspx[/bold]

<html>
<head></head>
<body>
<h1>custom error page</h1>
</body>
</html>



[bold]404page.aspx --Capture all 404(Not Found pages)[/bold]

<html>
<head></head>
<body>
<h1>404 error page</h1>
</body>
</html>



[bold]403page.aspx --Capture all 403(Restricted pages)[/bold]

<html>
<head></head>
<body>
<h1>403 error page</h1>
</body>
</html>



[bold]Step 4[/bold] After all webs are created, web.config 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. http://localhost/eventlog/customerr...g/eventlog.aspx [bold]Step 5[/bold] Verify the log was created and entry was placed in that log. Below is five possible types of Event log messages: [bold]Error[/bold] An error event. This indicates a significant problem the user should know about; usually a loss of functionality or data. [bold]FailureAudit[/bold] 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. [bold]Information[/bold] An information event. This indicates a significant, successful operation. [bold]SuccessAudit[/bold] A success audit event. This indicates a security event that occurs when an audited access attempt is successful; for example, logging on successfully. [bold]Warning[/bold] A warning event. This indicates a problem that is not immediately significant, but that may signify conditions that could cause future problems. 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!!

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Complete example using custom errors and write errors to the Event log


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway