locked
Exception Handling RRS feed

  • Question

  • User-647359487 posted

    I have an HttpModule to handle logging for my web application, but every error gets logged as the following:

    Exception of type 'System.Web.HttpUnhandledException' was thrown.

    Which is pretty useless when I know an actual exception was thrown.   

    Here's what I'm doing on my Application_Error event via my module:

      

    1    Exception ex = HttpContext.Current.Server.GetLastError();
    2    // Get the current user if logged in, returns empty Guid if not
    3    Guid userId = GetUserIdOfLoggedInUser();
    4    // Write the exception to the log
    5    Guid errorId = LogFactory.WriteExceptionToLog(GetDatabaseConnectionString(), ex, userId);
    

    What would be the reason that the exception always gets logged as that?
     

    Friday, September 22, 2006 9:36 AM

Answers

  • User1254222884 posted

    It's being reported that way because that's the object you've got.  At the System.GetLastError, when there's an unhandled error inside the asp code, that is the error that's returned. To get to the actual error, you have to drill down one more level.

    Exception ex = HttpContext.Current.Server.GetLastError().InnerException;

    That will return you the error at the code level.

    Cheers,
    Owen

    I have an HttpModule to handle logging for my web application, but every error gets logged as the following:

    Exception of type 'System.Web.HttpUnhandledException' was thrown.

    Which is pretty useless when I know an actual exception was thrown.   

    Here's what I'm doing on my Application_Error event via my module:

      

    1    Exception ex = HttpContext.Current.Server.GetLastError();
    2    // Get the current user if logged in, returns empty Guid if not
    3    Guid userId = GetUserIdOfLoggedInUser();
    4    // Write the exception to the log
    5    Guid errorId = LogFactory.WriteExceptionToLog(GetDatabaseConnectionString(), ex, userId);
    

    What would be the reason that the exception always gets logged as that?
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 22, 2006 12:40 PM

All replies

  • User1254222884 posted

    It's being reported that way because that's the object you've got.  At the System.GetLastError, when there's an unhandled error inside the asp code, that is the error that's returned. To get to the actual error, you have to drill down one more level.

    Exception ex = HttpContext.Current.Server.GetLastError().InnerException;

    That will return you the error at the code level.

    Cheers,
    Owen

    I have an HttpModule to handle logging for my web application, but every error gets logged as the following:

    Exception of type 'System.Web.HttpUnhandledException' was thrown.

    Which is pretty useless when I know an actual exception was thrown.   

    Here's what I'm doing on my Application_Error event via my module:

      

    1    Exception ex = HttpContext.Current.Server.GetLastError();
    2    // Get the current user if logged in, returns empty Guid if not
    3    Guid userId = GetUserIdOfLoggedInUser();
    4    // Write the exception to the log
    5    Guid errorId = LogFactory.WriteExceptionToLog(GetDatabaseConnectionString(), ex, userId);
    

    What would be the reason that the exception always gets logged as that?
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 22, 2006 12:40 PM
  • User-647359487 posted
    Thanks Owen, figures it'd be something simple.
    Friday, September 22, 2006 1:02 PM
  • User1254222884 posted
    Now, if I could just figure out where the HTTP Error code is hiding (such as 404 when the file is not found), then I'll have my problem solved.
    Friday, September 22, 2006 1:08 PM