Search results
PDF

Error logging in ASP.NET Core Report Viewer

If an error occurred in report processing, ASP.NET Core Report Viewer displays short messages about the error in view. You need to configure the Controller to save all logs, stack trace, and error information into a physical file location.

This section explains how to log the detailed error information to your ASP.NET Core application.

This section requires a ASP.NET Core Report Viewer application, if you don’t have, then create by referring to the 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 : Controller, 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 application folder.

            internal void WriteLogs(string errorMessage)
            {
               string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "ErrorDetails.txt");
                using (StreamWriter writer = new StreamWriter(filePath, true))
                {
                    writer.AutoFlush = true;
                    writer.WriteLine(errorMessage);
                }
            }
  4. Invoke the newly created function in LogError as below.

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

    In cases of any issues faced in the report rendering, share the log file to our technical support team to get assistance on that.

  5. The final controller given as follows, you can replace it in your application.

    public class ReportViewerController : Controller, 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 = Path.Combine(_hostingEnvironment.WebRootPath, "ErrorDetails.txt");
            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.AutoFlush = true;
                writer.WriteLine(errorMessage);
            }
        }
    }