Can’t retrieve any message to the Sentry page

Viewed 208

Goal:
Enable to track data whether it is error or not.

Problem:
I tried follow the instruction and when I execute the code, I still can’t get any error at sentry.io’s webpage.

What part am I missing?

Info:
*I’m newbie in Sentry
*I’m using the code from this picture

Thank you!

using Sentry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

// https://www.tutorialsteacher.com/csharp/custom-exception-csharp

namespace SentryTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            Student newStudent = null;

            try
            {
                newStudent = new Student();
                newStudent.StudentName = "James007";

                ValidateStudent(newStudent);
            }
            catch (InvalidStudentNameException ex)
            {

                using (SentrySdk.Init("https://472b09695026458c93638076efb9d666@o443014.ingest.sentry.io/5415884"))
                {
                    Console.WriteLine(ex.Message);
                }


            }


        }

        private static void ValidateStudent(Student std)
        {
            Regex regex = new Regex("^[a-zA-Z]+$");

            if (!regex.IsMatch(std.StudentName))
                throw new InvalidStudentNameException(std.StudentName);

        }

    }


    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
    }


    [Serializable]
    class InvalidStudentNameException : Exception
    {
        public InvalidStudentNameException()
        {

        }

        public InvalidStudentNameException(string name)
            : base(String.Format("Invalid Student Name: {0}", name))
        {

        }

    }



}

enter image description here

1 Answers

The docs is trying to point out that all the application code has to be inside the using block. That is because before your app exits, the Sentry SDK wants have a chance to flush any queued events to Sentry's server.

Since WPF apps don't have a Main method, you can't really use the using approach, you'll need to hold a reference to the IDisposable returned from SentrySdk.Init.

You can call SentrySdk.Init in the constructor of the App class, and save the reference returned as a private field. Then when the app terminates, you call Dispose on it.

Something like:


private readonly IDisposable _sentry;

public App()
{
    // get your Sentry DSN here somehow, i.e.:
    var dsn = ConfigurationManager.AppSettings["SentryDsn"];
    _sentry = SentrySdk.Init(dsn);
}

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    // this makes sure Sentry sends out any queued events before the app exits
    _sentry.Dispose();
}

Now you can call SentrySdk.CaptureException or any other method of the Sentry SDK throughout your app. The documentation on the Sentry website has more information.

Related