Handling Exceptions

Discussion in 'ASP .Net' started by Shahar Nechmad, Oct 18, 2006.

  1. I want to be able to log all exceptions that occur in the application (I'm
    using Enterprise Library and ASP.NET 2.0). Because with almost all exception
    I'm not going to do anything besides logging, I prefer dealing with it in
    one central place instead of writing try catch statements all over the
    place.
    I know I can catch all the application exception using the Application_Error
    event in the Golab.asax file, but the method doesn't seem to get the
    exception as parameter.

    How can I log all exceptions from one central place?
     
    Shahar Nechmad, Oct 18, 2006
    #1
    1. Advertising

  2. You're on the right track...

    Here's the code from an HttpModule I use (you can put it in your global.asax
    if you want):

    public void application_Error(object sender, EventArgs e)
    {
    HttpContext ctx = HttpContext.Current;
    //get the inner most exception
    Exception exception;
    for (exception = ctx.Server.GetLastError(); exception.InnerException !=
    null; exception = exception.InnerException) { }
    if (exception is HttpException &&
    ((HttpException)exception).GetHttpCode() == 404)
    {
    logger.Warn(string.Format("A 404 occurred:
    {0}",((HttpApplication)sender).Request.Url.ToString() ), exception );
    }
    else
    {
    logger.Error(string.Format("ErrorModule caught an unhandled exception
    on page {0}", ((HttpApplication)sender).Request.Url.ToString() ),
    exception);
    }
    ctx.Server.ClearError();
    ctx.Response.Redirert("error.aspx");
    }

    I'm using Log4net, but that's just a matter of changing the "logger.Warn"
    and "logger.Error"



    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Shahar Nechmad" <> wrote in message
    news:...
    >I want to be able to log all exceptions that occur in the application (I'm
    >using Enterprise Library and ASP.NET 2.0). Because with almost all
    >exception I'm not going to do anything besides logging, I prefer dealing
    >with it in one central place instead of writing try catch statements all
    >over the place.
    > I know I can catch all the application exception using the
    > Application_Error event in the Golab.asax file, but the method doesn't
    > seem to get the exception as parameter.
    >
    > How can I log all exceptions from one central place?
    >
    >
     
    Karl Seguin [MVP], Oct 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raterus
    Replies:
    6
    Views:
    596
    Raterus
    Aug 24, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,126
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,065
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    637
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    715
Loading...

Share This Page