locked
Error Event not being called in HttpModule RRS feed

  • Question

  • User-33878607 posted

    Hi

    I have create below HttpModule that will catch exceptions in the Web Appllication:

     

    namespace MyModule
    {

    public class MyClass:IHttpModule
        {
         
    public void Init(HttpApplication context)
            {
               
                context.Error += new EventHandler(context_Error);
            }

            void context_Error(object sender, EventArgs e)
            {

                //// Get the Last Error in the system

                Exception ex = HttpContext.Current.Server.GetLastError();
              //Logic to log exception object ex in text file.

            }
    }

    }

    Registered in Web Site web.config as follows:

     

     <httpModules>
     <add name="MyModule" type="MyModule.MyClass, MyModule"/>
     </httpModules>

     

    Asp.net code, i have caused exception purpusefully in asp.net code as follows:

     protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                int a = 1;
                int b = 0;
                int c = a / b;
            }
            catch (Exception ex)
            {

            }
          

        }

     

    Actually i am logging exceptions in text file . But when excpetion occurs in asp.net web site nothing is being logged.

     

    Cheers

    Happy Programming

     

     

     

    Sunday, October 30, 2011 3:55 AM

Answers

  • User629308654 posted

    For example : Error should be throw from page, only then you can track here, instand of if you are handled in page using try..catch block. Those error will not be considered.

    Code : In your Page :

    Try this

    try

    {

     /// your code...

    }

    catch(Expection ex)

    {

        throw;

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 3, 2011 8:01 AM