Error logging in ASP.NET Core Report Writer
To log errors from the Bold Reports® ASP.NET Core Report Writer, register the ReportErrorOccurred event, which the Report Writer raises when an error occurs in report processing. Get the report error details from the event arguments and save the logs, stack trace, and error information to a physical file location.
Follow these steps to log detailed error information in your ASP.NET Core application.
You need an ASP.NET Core Report Writer application. If you do not have one, create it by following the ASP.NET Core Report Writer getting started guide.
-
In Solution Explorer, open the Report Writer Controller file.
-
Add the following sample code to register the report errors in the
ReportErrorOccurredevent.[HttpPost] public IActionResult Export(string writerFormat) { BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(); writer.ReportErrorOccurred += Writer_ReportErrorOccurred; } private void Writer_ReportErrorOccurred(object sender, ReportErrorOccurredEventArgs e) { // You can register the report errors using event arguments. } -
Create a method in
HomeControllerto 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
ReportErrorOccurredas follows:private void Writer_ReportErrorOccurred(object sender, ReportErrorOccurredEventArgs e) { WriteLogs(string.Format("Class Name: {0} \n Method Name: {1} \n Error Message: {2}", e.ClassName, e.MethodName, e.Message)); }If you face any issues in report rendering, share the log file with the Bold Reports® technical support team for help.
-
Replace the controller in your application with the following final controller code.
[HttpPost] public IActionResult Export(string writerFormat) { // Here, we have loaded the sales-order-detail sample report from application the folder wwwroot\Resources. FileStream inputStream = new FileStream(_hostingEnvironment.WebRootPath + @"\Resources\sales-order-detail.rdl", FileMode.Open, FileAccess.Read); MemoryStream reportStream = new MemoryStream(); inputStream.CopyTo(reportStream); reportStream.Position = 0; inputStream.Close(); BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(); writer.ReportErrorOccurred += Writer_ReportErrorOccurred; string fileName = null; WriterFormat format; string type = null; if (writerFormat == "PDF") { fileName = "sales-order-detail.pdf"; type = "pdf"; format = WriterFormat.PDF; } else if (writerFormat == "Word") { fileName = "sales-order-detail.docx"; type = "docx"; format = WriterFormat.Word; } else if (writerFormat == "CSV") { fileName = "sales-order-detail.csv"; type = "csv"; format = WriterFormat.CSV; } else { fileName = "sales-order-detail.xlsx"; type = "xlsx"; format = WriterFormat.Excel; } writer.LoadReport(reportStream); MemoryStream memoryStream = new MemoryStream(); writer.Save(memoryStream, format); // Download the generated export document to the client side. memoryStream.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + type); fileStreamResult.FileDownloadName = fileName; return fileStreamResult; } private void Writer_ReportErrorOccurred(object sender, ReportErrorOccurredEventArgs e) { WriteLogs(string.Format("Class Name: {0} \n Method Name: {1} \n Error Message: {2}", e.ClassName, e.MethodName, e.Message)); } 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); } }