How to get file name of a page where error happened?

Topics: Exception Handling Application Block, Logging Application Block
Aug 31, 2012 at 3:06 AM


We are using Enterprise Library 5.

We are using Exception Handling and Logging it in our db.

When we get following error. We could not find file name where error happened.

An exception of type 'System.ArgumentException'  occurred and was caught.  ------------------------------------------------------------------------  Type : System.ArgumentException, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089  Message : Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.

Which settings do we have to modify/use?

Best Regards,


Sep 1, 2012 at 5:25 AM

There are 3 issues that need to be addressed:

  1. Get the page name
  2. Pass the page name into the exception handler
  3. Save the page name in the database

The bad news is that, unfortunately, none of these steps is particularly simple.  The good news is that it is doable.

I'm assuming you are using a global exception handler such as:

protected void Application_Error()
    Exception error = Server.GetLastError();

    if (error is HttpUnhandledException)
        error = error.InnerException;

    Exception errorToThrow;

    if (ExceptionPolicy.HandleException(error, "Application Error", out errorToThrow))
        Response.Redirect(string.Format("ErrorPage.aspx?Message={0}", Uri.EscapeDataString((errorToThrow ?? error).Message)));   

Or something similar.

Retrieve Page Name

I didn't see a sure-fire way of obtaining the executing page.  One way that is probably good enough is using:


Pass the page name into the exception handler

An easy way to pass information into an exception handler is to use the Data IDictionary since this will get added to the LogEntry's ExtendedProperties.  The bad news is that the out of the box Database trace listener does not log the ExtendedProperties.

Save the page name in the database

Since the default database trace listener does not log the ExtendedProperties, you could use the Extended Properties Trace Listener from the Samples Page or you could add a column to the Log table to support the extra information (see the Custom Database Trace Listener Sample also on the sample page for that approach).  If using a custom trace listener you could also have the trace listener check if there is an HttpContext and if so to retrieve the information you need and save it to the database.

Randy Levy
Enterprise Library support engineer