Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global.asax - Application_Error - How can I get Page data?

I have this code:

using System.Configuration;

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();

    string ErrorMessage = ex.Message;
    string StackTrace = ex.StackTrace;
    string ExceptionType = ex.GetType().FullName;
    string UserId = Getloggedinuser();
    string WebErrorSendEmail =
       ConfigurationManager.AppSettings["WebErrorSendEmail"];

    // save the exception in DB
    LogStuffInDbAndSendEmailFromDb();
}

This is (most of) my code. In a small percentage of cases, I don't get enough information though. I don't know what page the exception originated from.

How can I get any kind of information related to the page that the exception originated from?

Below is an example of the shortest message:

Invalid length for a Base-64 char array.

at System.Convert.FromBase64String(String s) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) at System.Web.UI.HiddenFieldPageStatePersister.Load()

like image 402
Ash Avatar asked Apr 05 '13 13:04

Ash


People also ask

How do I catch exceptions in global ASAX?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

How do I get global ASAX file?

How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template. After you have added the global.

What is global ASAX page?

Global. asax is the asp.net application file. It is an optional file that handles events raised by ASP.NET or by HttpModules. Mostly used for application and session start/end events and for global error handling.


1 Answers

You can get the current request's URL and page like this :

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (HttpContext.Current != null)
    {
        var url = HttpContext.Current.Request.Url;
        var page = HttpContext.Current.Handler as System.Web.UI.Page;
    }
}
like image 153
Netricity Avatar answered Sep 29 '22 16:09

Netricity