Complete example using custom errors and write errors to the Event logWriting 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. |
| Step 1First of all the steps I followed was to create a simple web called eventlog. I also created the application rootso the global.asax file would fire.
Step 2I opened the global web.config file and turned on custom errors. Path to this is c:winntmicrosoft.netframework.. 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.
web.config -- this file is placed in the root of the eventlog application root. <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> Step 3The 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. Global.asax Page - This uses the Application_OnError event to capture stuff if an error happens <%@ 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> Default.aspx page <% @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> Customerrorpage.aspx <html> <head></head> <body> <h1>custom error page</h1> </body> </html>
404page.aspx --Capture all 404(Not Found pages) <html> <head></head> <body> <h1>404 error page</h1> </body> </html>
403page.aspx --Capture all 403(Restricted pages) <html> <head></head> <body> <h1>403 error page</h1> </body> </html> Step 4After 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/customerrorpage.aspx?aspxerrorpath=/eventlog/eventlog.aspx The page will appear like this Step 5. Verify the log was created and entry was placed in that log. The example shows a Critical error message. You also can show informational or yellow warning messages. For this example we showed all 5 possible entries
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!!
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!!
Parameters for WriteEntry Method- source
- The source by which the application is registered on the specified computer.
- message
- The string to write to the event log.
- type
- One of the EventLogEntryType values.
- eventID
- The application-specific identifier for the event.
- category
- The application-specific subcategory associated with the message.
- rawData
- An array of bytes that holds the binary data associated with the entry.
Five possible types of Event log messages. These show the different types of icons | Member Name | Description |
|---|
| Error | An error event. This indicates a significant problem the user should know about; usually a loss of functionality or data. | | FailureAudit | 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. | | Information | An information event. This indicates a significant, successful operation. | | SuccessAudit | A success audit event. This indicates a security event that occurs when an audited access attempt is successful; for example, logging on successfully. | | Warning | A warning event. This indicates a problem that is not immediately significant, but that may signify conditions that could cause future problems. | |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Articles More By aspfree developerWorks - FREE Tools! | David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
| | | | Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
| | | | Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |