ASP.NET
  Home arrow ASP.NET arrow Using custom errors and write errors to th...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Using custom errors and write errors to the Event log
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 6
    2001-07-14

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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!

    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 1

    First 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 2

    I 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 3

    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.

    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 4

    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/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 NameDescription
    ErrorAn error event. This indicates a significant problem the user should know about; usually a loss of functionality or data.
    FailureAuditA 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.
    InformationAn information event. This indicates a significant, successful operation.
    SuccessAuditA success audit event. This indicates a security event that occurs when an audited access attempt is successful; for example, logging on successfully.
    WarningA 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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Discovering the value of WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!


    NEW! Don't wait! Try the Rational Application Developer (RAD) v7.5 open beta code today

    Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Software Analyzer V7.0

    Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle.
    FREE! Go There Now!


    NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

    Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Lotus Forms V3.0

    Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Webcast: What is new in Viper 2 for developers?

    Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek