Error logging in ASP.NET Web Forms Report Writer
To log errors from the Bold Reports® ASP.NET Web Forms 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 Web Forms application.
You need an ASP.NET Web Forms Report Writer application. If you do not have one, create it by following the ASP.NET Web Forms Report Writer getting started guide.
-
In Solution Explorer, open the
Default.aspx.csfile. -
Add the following sample code to register the report errors in the
ReportErrorOccurredevent.protected void ExportButton_Click(object sender, EventArgs e) { 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
Default.aspx.csto write the error text into application folder.private void WriteLogs(string errorMessage) { string filePath = Path.Combine(writer.ReportPath, "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 event in your application with the following final event code.
protected void ExportButton_Click(object sender, EventArgs e) { // Here, we have loaded the sales-order-detail sample report from application the folder Resources. FileStream reportStream = new FileStream(Server.MapPath("~/Resources/sales-order-detail.rdl"), FileMode.Open, FileAccess.Read); BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(); writer.ReportErrorOccurred += Writer_ReportErrorOccurred; string fileName = null; WriterFormat format; string type = null; if (this.ExportFormat.SelectedValue == "PDF") { fileName = "sales-order-detail.pdf"; format = WriterFormat.PDF; } else if (this.ExportFormat.SelectedValue == "Word") { fileName = "sales-order-detail.docx"; format = WriterFormat.Word; } else if (this.ExportFormat.SelectedValue == "Html") { fileName = "sales-order-detail.Html"; format = WriterFormat.HTML; } else if (this.ExportFormat.SelectedValue == "PPT") { fileName = "sales-order-detail.ppt"; format = WriterFormat.PPT; } else { fileName = "sales-order-detail.xlsx"; format = WriterFormat.Excel; } writer.LoadReport(reportStream); HttpContext httpContext = System.Web.HttpContext.Current; writer.Save(fileName, format, httpContext.Response); } } 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)); } private void WriteLogs(string errorMessage) { string filePath = Path.Combine(Server.MapPath("~/Resources/ErrorDetails.txt")); using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.AutoFlush = true; writer.WriteLine(errorMessage); } }