Search results
Suggest a FeaturePDF

Error logging in WPF Report Viewer

If an error occurred in report processing, WPF Report Viewer displays short messages about the error in view. In report viewer ReportError event raised. You can register the event and get the report error details from the event arguments to save all logs error information into a physical file location.

This section explains how to log the detailed error information to your WPF application.

This section requires a WPF Report Viewer application, if you don’t have then create using the Getting-Started.

  1. In Solution Explorer, Open 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 follows.

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

    In cases of any issues faced in the report rendering, share the log file to our technical support team to get assistance on that.

  5. The final MainWindow.xaml.cs file is given as follows, you can replace it in your application.

    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);
            }
        }
    }
Having trouble getting help?
Contact Support
Having trouble getting help?
Contact Support