Back to Devexpress

Handle and Log Server-Side Errors in ASP.NET MVC

dashboard-400025-web-dashboard-integrate-dashboard-component-aspnet-mvc-dashboard-extension-handle-and-log-server-side-errors-in-asp-net-mvc.md

latest4.2 KB
Original Source

Handle and Log Server-Side Errors in ASP.NET MVC

  • Jun 06, 2023
  • 3 minutes to read

The ASP.NET MVC 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 MVC Dashboard - How to handle errors

Catch Unhandled Exceptions

  1. Open the Global.asax file in your ASP.NET MVC 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:

Catch Connection Errors

Use the DashboardConfigurator.ConnectionError event to catch and log connection errors. The ConnectionError event occurs if connection to a data store fails with current parameter values (for instance, the database is inaccessible).

  1. Open the App_Start\DashboardConfig.cs file in your ASP.NET MVC project.

  2. In the RegisterService method use the ConnectionErrorWebEventHandler to subscribe to the DashboardConfigurator.ConnectionError event:

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 and save information about the root cause of the issue.

  2. Use the created AddToLog method to log the exception information to a text file.

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.

xml
<configuration>
  
    <customErrors mode="On|Off|RemoteOnly">
    </customErrors>
  
</configuration>

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

ValueDescription
OnThe 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.
OffThe Web Dashboard control provides detailed errors for remote clients and the local host.
RemoteOnlyThe 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:

xml
<configuration>
  
    <!-- ... -->
    <httpErrors errorMode="Detailed" />
  
</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:

xml
<configuration>
  <location path="dashboardControl">
    
      <!-- ... -->
      <httpErrors errorMode="Detailed" />
    
  </location>
</configuration>