Exemplo n.º 1
0
    public async Task RecordsSql()
    {
        Skip.If(!RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

        var transport = new RecordingTransport();
        var options   = new SentryOptions
        {
            TracesSampleRate = 1,
            Transport        = transport,
            Dsn             = DsnSamples.ValidDsnWithoutSecret,
            DiagnosticLevel = SentryLevel.Debug
        };

        options.AddIntegration(new SentryDiagnosticListenerIntegration());

        using var database = await sqlInstance.Build();

        using (var hub = new Hub(options))
        {
            var transaction = hub.StartTransaction("my transaction", "my operation");
            hub.ConfigureScope(scope => scope.Transaction = transaction);
            hub.CaptureException(new Exception("my exception"));
            await TestDbBuilder.AddData(database);

            await TestDbBuilder.GetData(database);

            transaction.Finish();
        }

        var payloads = transport.Envelopes
                       .SelectMany(x => x.Items)
                       .Select(x => x.Payload)
                       .ToList();

        await Verify(payloads)
        .ModifySerialization(
            p =>
        {
            p.IgnoreMembersWithType <Contexts>();
            p.IgnoreMembersWithType <SdkVersion>();
            p.IgnoreMembersWithType <DateTimeOffset>();
            p.IgnoreMembersWithType <SpanId>();
            p.IgnoreMembersWithType <SentryId>();
            p.IgnoreMembers <SentryEvent>(e => e.Modules, e => e.Release);
            p.IgnoreMembers <Transaction>(t => t.Release);
            p.IgnoreMembers <SentryException>(e => e.Module, e => e.ThreadId);
        });
    }
    /// <summary>
    /// Adds the entity framework integration.
    /// </summary>
    /// <param name="sentryOptions">The sentry options.</param>
    public static SentryOptions AddEntityFramework(this SentryOptions sentryOptions)
    {
        try
        {
            _ = SentryDatabaseLogging.UseBreadcrumbs(diagnosticLogger: sentryOptions.DiagnosticLogger);
        }
        catch (Exception e)
        {
            sentryOptions.DiagnosticLogger?
            .LogError("Failed to configure EF breadcrumbs. Make sure to init Sentry before EF.", e);
        }

        DbIntegration = new DbInterceptionIntegration();
        sentryOptions.AddIntegration(DbIntegration);

        sentryOptions.AddExceptionProcessor(new DbEntityValidationExceptionProcessor());
        // DbConcurrencyExceptionProcessor is untested due to problems with testing it, so it might not be production ready
        //sentryOptions.AddExceptionProcessor(new DbConcurrencyExceptionProcessor());
        return(sentryOptions);
    }
Exemplo n.º 3
0
        private static void Main()
        {
            var sentryOptions = new SentryOptions
            {
                Dsn         = "https://[email protected]/5755327",
                Environment = AssemblyUtils.GetReleaseState().ToString()
            };
            var contribOptions = new ContribSentryOptions()
            {
                GlobalSessionMode = true,
                DistinctId        = AppConfigs.Configuration.UniqueInstallationId.ToString()
            };

            sentryOptions.AddIntegration(new ContribSentrySdkIntegration(contribOptions));
            using var _ = SentrySdk.Init(sentryOptions);

            SentrySdk.ConfigureScope(scope => scope.User = new User
            {
                Id       = AppConfigs.Configuration.UniqueInstallationId.ToString(),
                Username = Environment.UserName
            });
            ContribSentrySdk.StartSession();
            InitializeLogger();
            Log.Information("Application Starts");
#if !DEBUG
            AppDomain.CurrentDomain.UnhandledException += (sender, args) => { HandleException((Exception)args.ExceptionObject); };

            Log.Information("Set Exception Handler");
            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            WindowsAPIAdapter.Start(Application_ThreadException);
#else
            WindowsAPIAdapter.Start();
#endif
            Thread.CurrentThread.CurrentUICulture = new LanguageFactory().Get(AppModel.Instance.Language).CultureInfo;
            Thread.CurrentThread.Name             = "Main Thread";
            var userMutexName = Application.ProductName + Environment.UserName;

            using var mainMutex = new Mutex(true, Application.ProductName);
            using var userMutex = new Mutex(true, userMutexName, out var userMutexHasOwnership);
            if (!userMutexHasOwnership)
            {
                Log.Warning("SoundSwitch is already running for this user {@Mutex}", userMutexName);
                WindowsAPIAdapter.Stop();
                Log.CloseAndFlush();
                return;
            }


            SetProcessDPIAware();

            Application.EnableVisualStyles();
#if NETCORE
            Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
#endif
            Application.SetCompatibleTextRenderingDefault(false);
            // Manage the Closing events send by Windows
            // Since this app don't use a Form as "main window" the app doesn't close
            // when it should without this.
            WindowsAPIAdapter.RestartManagerTriggered += (sender, @event) =>
            {
                Log.Debug("Restart Event received: {Event}", @event);
                switch (@event.Type)
                {
                case WindowsAPIAdapter.RestartManagerEventType.Query:
                    @event.Result = new IntPtr(1);

                    break;

                case WindowsAPIAdapter.RestartManagerEventType.EndSession:
                case WindowsAPIAdapter.RestartManagerEventType.ForceClose:
                    Log.Debug("Close Application");
                    Environment.Exit(0);
                    break;
                }
            };

            Log.Information("Set Tray Icon with Main");
#if !DEBUG
            try
            {
#endif
            MMNotificationClient.Instance.Register();


            using var ctx = new WindowsFormsSynchronizationContext();

            SynchronizationContext.SetSynchronizationContext(ctx);
            try
            {
                Application.Run(new SoundSwitchApplicationContext());
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(null);
            }


#if !DEBUG
        }

        catch (Exception ex)
        {
            HandleException(ex);
        }
#endif
            AppModel.Instance.Dispose();
            WindowsAPIAdapter.Stop();
            MMNotificationClient.Instance?.Dispose();
            ContribSentrySdk.EndSession();
            Log.CloseAndFlush();
        }
 /// <summary>
 /// Attach Sentry to System DiagnosticSource.
 /// </summary>
 /// <param name="options">The Sentry options.</param>
 public static void AddDiagnosticSourceIntegration(this SentryOptions options)
 => options.AddIntegration(new SentryDiagnosticListenerIntegration());