Error logging in JavaScript Report Viewer
To log detailed error information from the Bold Reports® JavaScript Report Viewer, configure the ApiController in the Web API service 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.
You need a JavaScript Report Viewer application to follow these steps. If you don’t have one, create it by following the JavaScript Report Viewer getting started documentation.
-
In Solution Explorer, open the Report Viewer Controller file.
-
Inherit the
IReportLoggerinterface 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(); } } -
Create a method in
ReportViewerControllerto 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); } } -
Invoke the newly created function in
LogError, as in the following code: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 issues in report rendering, share the log file with the Bold Reports® technical support team for assistance.
-
Replace the controller in your application with the following final controller.
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, message, 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); } } }