Error logging in ASP.NET Web Forms Report Viewer

To log detailed error information from the Bold Reports® ASP.NET Web Forms Report Viewer, configure the ApiController to save all logs, stack trace, and error information into a physical file location. When an error occurs in report processing, the Report Viewer displays only a short message about the error in the view.

The following steps log the detailed error information in your ASP.NET Web Forms application.

You need an ASP.NET Web Forms Report Viewer application to follow these steps. If you don’t have one, create it by following the ASP.NET Web Forms Report Viewer getting started documentation.

  1. In Solution Explorer, open the Report Viewer Controller file.

  2. Inherit the IReportLogger interface and implement the interface methods.

    public class ReportViewerController : ApiController, IReportController, IReportLogger
    {
        public void LogError(string message, Exception exception, MethodBase methodType, ErrorType errorType)
        {
            throw new NotImplementedException();
        }
    
        public void LogError(string errorCode, string message, Exception exception, string errorDetail, string methodName, string className)
        {
            throw new NotImplementedException();
        }  
    }
  3. Create a method in ReportViewerController to write the error text into the application folder.

            internal void WriteLogs(string errorMessage)
            {
                string filePath = HttpContext.Current.Server.MapPath("/App_Data/Errordetails.txt");
                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.AutoFlush = true;
                    writer.WriteLine(errorMessage);
                }
            }
  4. Invoke the newly created function in LogError as follows:

        public void LogError(string message, Exception exception, MethodBase methodType, ErrorType errorType)
        {
            WriteLogs(string.Format("Error Message: {0} \n Stack Trace: {1}", message, exception.StackTrace));
        }
    
        public void LogError(string errorCode, string message, Exception exception, string errorDetail, string methodName, string className)
        {
            WriteLogs(string.Format("Class Name: {0} \n Method Name: {1} \n Error Message: {2} \n Stack Trace: {3}", className, methodName, errorDetail, exception.StackTrace));
        }

    If you face any issue in report rendering, share the log file with the Bold Reports® technical support team to get assistance.

  5. The final controller is given as follows. You can replace it in your application.

    public class ReportViewerController : ApiController, IReportController, IReportLogger
    {
        // Post action for processing the RDL/RDLC report
        public object PostReportAction(Dictionary<string, object> jsonResult)
        {
            return ReportHelper.ProcessReport(jsonResult, this);
        }
    
        // Get action for getting resources from the report
        [System.Web.Http.ActionName("GetResource")]
        [AcceptVerbs("GET")]
        public object GetResource(string key, string resourcetype, bool isPrint)
        {
            return ReportHelper.GetResource(key, resourcetype, isPrint);
        }
    
        // Method that will be called when initialize the report options before start processing the report
        [NonAction]
        public void OnInitReportOptions(ReportViewerOptions reportOption)
        {
            // You can update report options here
        }
    
        // Method that will be called when reported is loaded
        [NonAction]
        public void OnReportLoaded(ReportViewerOptions reportOption)
        {
            // You can update report options here
        }
    
        public void LogError(string message, Exception exception, MethodBase methodType, ErrorType errorType)
        {
                WriteLogs(string.Format("Error Message: {0} \n Stack Trace: {1}", message, exception.StackTrace));
        }
    
        public void LogError(string errorCode, string message, Exception exception, string errorDetail, string methodName, string className)
        {
            WriteLogs(string.Format("Class Name: {0} \n Method Name: {1} \n Error Message: {2} \n Stack Trace: {3}", className, methodName, errorDetail, exception.StackTrace));
        }
    
        internal void WriteLogs(string errorMessage)
        {
            // Error details text file path location
            string filePath = HttpContext.Current.Server.MapPath("/App_Data/Errordetails.txt");
            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.AutoFlush = true;
                writer.WriteLine(errorMessage);
            }
        }
    }