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.

  1. In Solution Explorer, open the Report Viewer initialization .cs file.

  2. Register the ReportError event.

    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.
    }
  3. Create a method in MainWindow.xaml.cs to 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);
                }
            }
  4. 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.

  5. Replace the file in your application with the following final MainWindow.xaml.cs file.

    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);
            }
        }
    }