Error logging in WPF Report Viewer
To log detailed error information from the Bold Reports® WPF Report Viewer, register the ReportError event and save the error details from the event arguments 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 WPF Report Viewer application to follow these steps. If you do not have one, create it by following the WPF Report Viewer getting started documentation.
-
In Solution Explorer, open the Report Viewer initialization
.csfile. -
Register the
ReportErrorevent.private void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.reportViewer.ReportError += ReportViewer_ReportError; } private void ReportViewer_ReportError(object sender, ReportErrorEventArgs e) { // You can register the report errors using event arguments. } -
Create a method in
MainWindow.xaml.csto write the error text into application folder.private void WriteLogs(string errorMessage) { string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+ @"\Errordetails.txt"; using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.AutoFlush = true; writer.WriteLine(errorMessage); } } -
Invoke the newly created function in
ReportViewer_ReportError, as in the following code:private void ReportViewer_ReportError(object sender, ReportErrorEventArgs e) { string errorLog; if (e.Exception != null) { errorLog = (string.Format("Error Message: {0} \n Stack Trace: {1}", e.Message, e.Exception.StackTrace)); } else { errorLog = e.Message; } WriteLogs(errorLog); }If you face any issues in report rendering, share the log file with the Bold Reports® technical support team for assistance.
-
Replace the file in your application with the following final
MainWindow.xaml.csfile.public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded); } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.reportViewer.ReportPath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\sales-order-detail.rdl"); this.ReportViewer.ReportError += ReportViewer_ReportError; this.reportViewer.RefreshReport(); } private void ReportViewer_ReportError(object sender, ReportErrorEventArgs e) { string errorLog; if (e.Exception != null) { errorLog = (string.Format("Error Message: {0} \n Stack Trace: {1}", e.Message, e.Exception.StackTrace)); } else { errorLog = e.Message; } WriteLogs(errorLog); } private void WriteLogs(string errorMessage) { string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+ @"\Errordetails.txt"; using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.AutoFlush = true; writer.WriteLine(errorMessage); } } }