Solved

Help implementing an error handling utility on web applicaiton

Posted on 2006-11-02
6
220 Views
Last Modified: 2012-06-27
Visual Studio 2003 with .NET 1.1.  (no 2.0 as of yet)

So far my experience with error reporting when working on a web application has been rather weak and recently I ran into this article
http://www.codeproject.com/aspnet/GlobalErrorHandler.asp
that sounds somewhat promising if it can be implemented quickly.  I am somewhat of a beginner so please do not assume much or at least explain with clear and thorough intructions.

What I understand is that the errorhandler function here can be compiled as its own standalone class and be incorporated into any web application project.  Could you please explain from beginning to end how to create this class and incorporate it into a web application?  The download file is unavailable and it seems that the article is missing a few steps if you were to create the class from scratch.  If you have a better suggestion for doing something similar please make it although I like the idea of having a web error reporting utility/class that I can add to webapp projects while in development.  

Thank you very much in advance.
0
Comment
Question by:wsbdc-questions
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17858596
Here is the source, it downloads fin for me :

using System;
using System.Web;
using System.Collections.Specialized;
using System.Reflection;
using System.Diagnostics;

namespace MyApps.Utilities
{
      public class GlobalErrorHandler : IHttpModule
      {
            public void Init (HttpApplication app)
            {
                  app.Error += new System.EventHandler (OnError);
            }

            public void OnError (object obj, EventArgs args)
            {
                  // At this point we have information about the error
                  HttpContext ctx = HttpContext.Current;
                  HttpResponse response = ctx.Response;
                  HttpRequest request = ctx.Request;

                  Exception exception = ctx.Server.GetLastError();

                  response.Write("Your request could not processed. Please press the back button on your browser and try again.<br/>");
                  response.Write("If the problem persists, please contact technical support<p/>");
                  response.Write("Information below is for technical support:<p/>");
                  
                  string errorInfo = "<p/>URL: " + ctx.Request.Url.ToString () + "<p/>Stacktrace:---<br/>" + exception.InnerException.StackTrace.ToString() + "<p/>Error Message:<br/>" + exception.InnerException.Message;

                  response.Write("Querystring:<p/>");

                  for(int i=0;i<request.QueryString.Count;i++)
                  {
                        response.Write("<br/>" + request.QueryString.Keys[i].ToString() + " :--" + request.QueryString[i].ToString() + "--<br/>");// + nvc.
                  }

                  response.Write("<p>-------------------------<p/>Form:<p/>");

                  for(int i=0;i<request.Form.Count;i++)
                  {
                        response.Write("<br/>" + request.Form.Keys[i].ToString() + " :--" + request.Form[i].ToString() + "--<br/>");// + nvc.
                  }

                  response.Write("<p>-------------------------<p/>ErrorInfo:<p/>");

                  response.Write (errorInfo);

                  // --------------------------------------------------
                  // To let the page finish running we clear the error
                  // --------------------------------------------------
                  ctx.Server.ClearError ();
            }

            public void Dispose () {}
      }
}
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17858614
You start a new C# project in Visual Studio of Type class library.
Paste the code above into it.
You will need to fix the namespaces etc.

Compile.
Put in the bin folder of the web app.
Modify the web.config as described.

Should work OK.

Can email you the source zip file if required.
0
 

Author Comment

by:wsbdc-questions
ID: 17859491
Thank you, that worked.  Now, could you provide an example of using the error handler?  I believe this handler works as a result of an application error.  Is there a way, perhaps by throwing an error, to put this class to use not only for error reporting but also for simple debugging?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17860761
Do you want a separator on a menu or a separator on a toolbar ?

The items go on the Menu in the order you add them the same with the toolbar.

If you want a separator on a toolbar add a button and set its style to separator.
If you want a separator on a menu add a menuitem and set its text to "-".

If you want to add either in a particlar place use the Insert() method instead of the Add() method and specify the index at which the item is to be inserted.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17861653
sorry, that last comment went in the wrong question.
0
 

Author Comment

by:wsbdc-questions
ID: 17901397
No problem.  I actually like this class a lot.  Is there a way to use it not only for error reporting but also for debugging?

0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question