If an error occurred in report processing, ASP.NET MVC Report writer ReportErrorOccurred
event raised. You can register the event and get the report error details from the event arguments 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 MVC application.
This section requires an ASP.NET MVC Report Writer application, if you don’t have, then create by referring to the Getting-Started documentation.
In Solution Explorer, open the HomeController
file.
Add the following sample code to register the report errors in the ReportErrorOccurred
event.
[HttpPost]
public ActionResult 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 HomeController
to write the error text into application folder.
internal void WriteLogs(string errorMessage)
{
string filePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath, "ErrorDetails.txt");
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.AutoFlush = true;
writer.WriteLine(errorMessage);
}
}
Invoke the newly created function in ReportErrorOccurred
as 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));
}
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 is given as follows, you can replace it in your application.
[HttpPost]
public ActionResult Export(string writerFormat)
{
// Here, we have loaded the sales-order-detail sample report from application the folder Resources.
FileStream reportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\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 (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));
}
private void WriteLogs(string errorMessage)
{
string filePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Resources\ErrorDetails.txt");
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.AutoFlush = true;
writer.WriteLine(errorMessage);
}
}