7

I have an Asp.Net 4.0 Web Forms application that throws following error certain times

“Server Error in ‘/MySiteDev’ Application” .

This error comes only at times. And this error is not firing the Application_Error event which is handled in Global.asax.

Since this is not firing Application_Error, what all are the other possible places that will have a log of this error event? Anything other than event viewer available?

Any way to find out the exceptions handled by the ASP.Net framework?

Note: customErrors mode="Off". Also runAllManagedModulesForAllRequests="true"

UPDATE

Reference from How to: Handle Application-Level Errors

An error handler that is defined in the Global.asax file will only catch errors that occur during processing of requests by the ASP.NET runtime. For example, it will catch the error if a user requests an .aspx file that does not occur in your application. However, it does not catch the error if a user requests a nonexistent .htm file. For non-ASP.NET errors, you can create a custom handler in Internet Information Services (IIS). The custom handler will also not be called for server-level errors.

You cannot directly output error information for requests from the Global.asax file; you must transfer control to another page, typically a Web Forms page. When transferring control to another page, use Transfer method. This preserves the current context so that you can get error information from the GetLastError method.

After handling an error, you must clear it by calling the ClearError method of the Server object (HttpServerUtility class).

CODE

    protected void Application_Error(object sender, EventArgs e)
    {
        //Get the exception object
        Exception exception = Server.GetLastError().GetBaseException();

        //Get the location of the exception
        string location = Request.Url.ToString();
        if (!String.IsNullOrEmpty(location))
        {
            string[] partsOfLocation = location.Split('/');
            if (partsOfLocation != null)
            {
                if (partsOfLocation.Length > 0)
                {
                    location = partsOfLocation[partsOfLocation.Length - 1];
                }
            }

            //Maximum allowed length for location is 255
            if (location.Length > 255)
            {
                location = location.Substring(0, 254);
            }
        }

        string connectionString = ConfigurationManager.ConnectionStrings[UIConstants.PayrollSQLConnection].ConnectionString;
        ExceptionBL exceptionBL = new ExceptionBL(connectionString);

        exceptionBL.SubmitException(exception.Message, location);
        Log.Logger.Error(exception.Message);

    }

CONFIG

<system.web>

<compilation debug="true" targetFramework="4.0" />
<pages validateRequest="false"></pages>
<httpRuntime requestValidationMode="2.0" />
<customErrors mode="Off"/>
<authentication mode="Windows"></authentication>
<identity impersonate="true" userName="domain\xxxx" password="xxxx"/>

</system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors errorMode="Detailed" />
</system.webServer>

UPDATED REFERENCES

  1. Application_Error not firing
  2. Global.asax event Application_Error is not firing
  3. Application_Error does not fire?
  4. How to: Handle Application-Level Errors
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

3

Examine logs in the Event Viewer, which should log both server-level and application-level errors.

The application error handler probably isn't processing the event because it's happening before your application is successfully started and a context created. So, there would seem to be an application configuration or server configuration ceasing processing the request.

Or, the application is encountering a problem early enough in the request lifecycle that even after starting, it's 'hung' until the server decides to kill the process (for instance, perhaps in the form of a StackOverflowException mentioned by @MikeSmithDev).

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 1
    Beat me to it! Also, could be StackOverflowException... I'd be interested to see OP details from event logs. – MikeSmithDev Feb 14 '13 at 14:20
  • @MikeSmithDev Following article says "be it a "stackoverflow" exception or a "404 Not found" it will end up in Application_Error." Are you saying this statement is incorrect? `StackOverflow exception` will not be caught by Application_Error? http://totaldotnet.com/Article/ShowArticle58_GlobleErrorHandle.aspx – LCJ Feb 14 '13 at 15:11
  • 1
    @Lijo yes that is what I'm saying. I just tested your code. It caught a regular Exception. It did not catch it when I made a StackOverflowException happen. Try/Catch cannot catch them either. The corresponding process is terminated immediately. If this happens too much, too often, the app_pool will be shutdown and your site will basically be dead until you recycle it. – MikeSmithDev Feb 14 '13 at 15:55
  • @MikeSmithDev What do you think about the answer that I added? Please leave a comment about your thoughts. – LCJ Feb 19 '13 at 10:19
  • What do you think about the answer that I added? Please leave a comment about your thoughts. – LCJ Feb 19 '13 at 10:19
  • @Lijo looks like you fixed it! – MikeSmithDev Feb 19 '13 at 14:26
1

This is a load balanced environment with A and B boxes.

The team who deployed the web application confirmed that in one of the boxes, the config file as not copied properly.

I think, the application worked well when it was hitting A box and failing in B box. I think, since the config is not there, it was not possible to call Application_Error.

Please tell if you have different opinion.

Note: The issue is not there when they re-deployed

LCJ
  • 22,196
  • 67
  • 260
  • 418