Error logging in UWP Report Viewer

To log detailed error information from the Bold Reports® UWP Report Viewer, handle the ReportError event and save the logs and error information 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 and raises the Reporterror event. You can register the event and get the report error details from the event arguments.

You need a UWP Report Viewer application to follow these steps. If you do not have one, create it by following the UWP Report Viewer getting started documentation.

  1. In Solution Explorer, Open report viewer initialization cs file.

  2. Register the ReportError event.

    private void MainPage_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 MainPage.xaml.cs to write the error text into application folder.

        private async void WriteLogs(string errorMessage)
        {
            var appFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            //you can refer to your preferred location path for errordetails text file.
            var filePath = await appFolder.CreateFileAsync("Errordetails.txt", Windows.Storage.CreationCollisionOption.OpenIfExists);
            await Windows.Storage.FileIO.AppendTextAsync(filePath, errorMessage + Environment.NewLine);
        }
  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 MainPage.xaml.cs file is given as follows, you can replace it in your application.

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }
    
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.ReportViewer.ReportPath = "~/Resources/docs/sales-order-dtail.rdl";
            this.ReportViewer.ReportServiceURL = @"https://demos.boldreports.com/services/api/ReportViewer";
            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 async void WriteLogs(string errorMessage)
        {
            var appFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            //you can refer to your preferred location path for errordetails text file.
            var filePath = await appFolder.CreateFileAsync("Errordetails.txt", Windows.Storage.CreationCollisionOption.OpenIfExists);
            await Windows.Storage.FileIO.AppendTextAsync(filePath, errorMessage + Environment.NewLine);
        }
    }