Skip to main content

Handle and Log Server-Side Errors in ASP.NET Web Forms

  • 6 minutes to read

The ASP.NET Web Forms Dashboard allows you to catch and log server-side exceptions that can occur when an incorrect dashboard is being loaded or data cannot be requested.

View Example: ASP.NET Web Forms Dashboard - How to handle errors

Catch Unhandled Exceptions

Use the ASPxWebControl.CallbackError event to catch server-side exceptions. This event is used for the ASPxDashboard and DashboardConfigurator settings.

  1. Open (or create) the Global.asax file in your ASP.NET Web Forms project.

  2. In the Application_Start method, handle the ASPxWebControl.CallbackError event and call the GetLastError method. As a result, the exception raises the HttpApplication.Error event:

    using DevExpress.Web;
    // ...
    
    protected void Application_Start() {
        // ...
        ASPxWebControl.CallbackError += Application_Error;
    }
    
    protected void Application_Error(object sender, EventArgs e) {
        Exception exception = HttpContext.Current.Server.GetLastError();
        // ...
    } 
    

Catch Connection Errors

The ConnectionError event fires when the current connection parameters do not allow you to connect to a data store (for instance, the database is inaccessible).

  1. If you use the ASPxDashboard settings to configure a control, handle the ASPxDashboard.ConnectionError event. For example, add an event subscription when the page loads:

    using DevExpress.DashboardWeb;
    // ...
    
    protected void Page_Load(object sender, EventArgs e) {
        // ...
        ASPxDashboard1.ConnectionError += ASPxDashboard_ConnectionError;         
    }
    
    protected void ASPxDashboard_ConnectionError(object sender, ConnectionErrorWebEventArgs e) {
        Exception exception = e.Exception;
        // ...
    }
    
  2. If you use the DashboardConfigurator settings to configure a control, handle the DashboardConfigurator.ConnectionError event:

    using DevExpress.DashboardWeb;
    // ...
    
    protected void Application_Start() {
        // ...
        DashboardConfigurator.Default.ConnectionError += Default_ConnectionError;
    }
    
    private static void Default_ConnectionError(object sender, ConnectionErrorWebEventArgs e) {
        Exception exception = e.Exception;
        // ...
    }
    

Log Error Details

  1. Create the TextLog class with the AddToLog method that adds a file with exception information to the specified path. The exception message is added to the end of the file if the log file already exists. The GetExceptionInfo method allows you to traverse through internal exceptions to save information about the issue’s cause:

    using System.IO;
    using System.Text;
    using DevExpress.DashboardCommon;
    //...
    
        public static class TextLog {
        public static void AddToLog(Exception exception, string path) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(DateTime.Now.ToLocalTime().ToString("F"));
            sb.AppendLine("Source File: " + System.Web.HttpContext.Current.Request.RawUrl);
            GetExceptionInfo(exception, sb);
            sb.AppendLine("------------------------------------------------------------" + Environment.NewLine);
            File.AppendAllText(path, sb.ToString());
        }
    
        private static void GetExceptionInfo(Exception exception, StringBuilder sb) {
            sb.AppendLine(exception.GetType().ToString());
            sb.AppendLine(exception.Message);            
            sb.AppendLine("Stack Trace: ");
            sb.AppendLine(exception.StackTrace);            
            if (exception is DashboardDataLoadingException) {
                foreach (var dataLoadingError in ((DashboardDataLoadingException)exception).Errors) {
                    sb.AppendLine("InnerException: ");
                    GetExceptionInfo(dataLoadingError.InnerException, sb);
                }
            }
            if (exception.InnerException != null) {
                sb.AppendLine("InnerException: ");
                GetExceptionInfo(exception.InnerException, sb);
            }
        }
    }
    
  2. Use the created AddToLog method to log the exception information to a text file:

    protected void Application_Error(object sender, EventArgs e) {
        TextLog.AddToLog(
            System.Web.HttpContext.Current.Server.GetLastError(), 
            System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Error.log"));
    }
    
    private static void Default_ConnectionError(object sender, ConnectionErrorWebEventArgs e) {
        TextLog.AddToLog(
            e.Exception, 
            System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Error.log"));
    }
    

Prevent Redirection on a Callback Error

When a server error occurs during the server-side callback process, ASPxHttpHandlerModule prevents an application from remaining pending by composing and sending a specific response to the client. When you use the callbackErrorRedirectUrl option within the Web.config file by specifying the page name, the response is redirected to the specified page. See Redirection on a Callback Error for more information.

<configuration>
    ...
    <devExpress>
        ...
        <errors callbackErrorRedirectUrl="" />
    </devExpress>
    ...
</configuration>

You can disable callbackErrorRedirectUrl only for the page containing the Web Dashboard. To do this, add the following code to your Web.config file:

<location path="<!-- a path to a page containing the dashboard control-->">
    <devExpress>
        <errors callbackErrorRedirectUrl="" />
    </devExpress>
</location>

Display Message Texts for Callback Errors

Like other controls produced by DevExpress, the Web Dashboard control allows specifying the message text when an unhandled exception occurs. This text might contain either exception details or a generic error message. To learn common ways of handling errors, see Callback Exception Handling.

The displayed error message depends on the mode attribute setting within the <customErrors> section in Web.config.

<configuration>
  <system.web>
    <customErrors mode="On|Off|RemoteOnly">
    </customErrors>
  </system.web>
</configuration>

The mode attribute can be set to one of the following values.

Value Description
On The Web Dashboard control provides only general information about errors. Generic error text is shown for remote clients and on the local host. This is a localizable text defined by the ASPxperienceStringId.CallbackGenericErrorText constant.
Off The Web Dashboard control provides detailed errors for remote clients and the local host.
RemoteOnly The generic error text is shown only to the remote clients, and detailed ASP.NET errors are shown on the local host.

To show a custom error message on the client when an exception is thrown, set the httpErrors.errorMode attribute to Detailed in the Web.config file:

<configuration>
  <system.webServer>
    <!-- ...  -->
    <httpErrors errorMode="Detailed" />
  </system.webServer>
</configuration>

If you do not need to enable detailed errors for the entire application, you can use the location directive to enable it only for the dashboard control:

<configuration>
  <location path="dashboardControl">
    <system.webServer>
      <!-- ...  -->
      <httpErrors errorMode="Detailed" />
    </system.webServer>
  </location>
</configuration>
See Also