Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error is not logged with Application_Error

Tags:

c#

.net

asp.net

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
like image 706
LCJ Avatar asked Feb 14 '13 14:02

LCJ


2 Answers

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).

like image 182
Grant Thomas Avatar answered Nov 15 '22 11:11

Grant Thomas


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

like image 21
LCJ Avatar answered Nov 15 '22 10:11

LCJ