Skip to main content
A newer version of this page is available. .

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

  • 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.

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:

    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

Use the DashboardConfigurator.ConnectionError event to catch and log connection errors. The ConnectionError event occurs when current connection parameters do not allow connecting to a data store (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:

    using DevExpress.DashboardWeb;
    // ...
    
    public static void RegisterService(RouteCollection routes) {
        // ...
        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 traversing through internal exceptions to save information about the root cause of the issue.

    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"));
    }
    

Logify

You can use the DevExpress Logify Alert tool for analyzing the application instead of the standard error handling methods.

Logify is an automated application-monitoring cloud service delivering crash reports to your centralized control panel. It enables you to set up report filters, trigger notifications manually, and add custom data to reports. You can also reroute notifications to a third-party logging service or your issue tracking tool of choice.

Refer to Logify Alert Documentation for information on how to get started with the Logify Alert service in ASP.NET MVC.