Error logging in Blazor Report Viewer
If an error occurred in report processing, Blazor 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 Blazor application.
This section requires a Blazor Report Viewer application, if you don’t have, then create by referring to the Getting-Started documentation.
-
In Solution Explorer, open the BoldReportsAPI Controller file.
-
Inherit the
IReportLoggerinterface and implement the interface methods.public class BoldReportsAPIController : ControllerBase, 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
BoldReportsAPIControllerto 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); } } -
Invoke the newly created function in
LogErroras 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.
-
The final controller given as follows, you can replace it in your application.
public class BoldReportsAPIController : ControllerBase, 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); } } }