Example #1
0
        public void Init_DsnInstance_EnablesSdk()
        {
            var dsn = new Dsn(DsnSamples.ValidDsnWithoutSecret);

            SentryCore.Init(dsn);
            Assert.True(SentryCore.IsEnabled);
        }
Example #2
0
    static void Main()
    {
        SentryCore.Init("https://[email protected]/id");

        // The following exception is captured and sent to Sentry
        throw null;
    }
Example #3
0
        /// <summary>
        /// Adds Sentry's services to the <see cref="IServiceCollection"/>
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="configureOptions">The configure options.</param>
        /// <returns></returns>
        public static IServiceCollection AddSentry(
            this IServiceCollection services,
            Action <SentryOptions> configureOptions)
        {
            services
            .AddSingleton <IRequestPayloadExtractor, FormRequestPayloadExtractor>()
            // Last
            .AddSingleton <IRequestPayloadExtractor, DefaultRequestPayloadExtractor>()
            .TryAddSingleton(p =>
            {
                if (configureOptions != null && !SentryCore.IsEnabled)
                {
                    var aspnetOptions = p.GetService <SentryAspNetCoreOptions>();
                    var options       = p.GetService <SentryOptions>();
                    options           = options ?? new SentryOptions();

                    aspnetOptions.InitSdk?.Invoke(options);
                    configureOptions?.Invoke(options);

                    var lifetime   = p.GetRequiredService <IApplicationLifetime>();
                    var disposable = SentryCore.Init(options);
                    lifetime.ApplicationStopped.Register(() => disposable.Dispose());
                }

                // TODO: SDK interface not accepting 'Integrations'
                // SentryCore.ConfigureScope(s => s.Sdk.AddIntegration(Constants.IntegrationName));

                // TODO: Need to fetch the created client and hub now to register in DI
                return(HubAdapter.Instance as IHub);
            });

            return(services);
        }
Example #4
0
        public void Init_MultipleCalls_ReplacesHubWithLatest()
        {
            var first = SentryCore.Init(ValidDsnWithSecret);

            SentryCore.AddBreadcrumb("test", "type");
            var called = false;

            SentryCore.ConfigureScope(p =>
            {
                called = true;
                Assert.Single(p.Breadcrumbs);
            });
            Assert.True(called);
            called = false;

            var second = SentryCore.Init(ValidDsnWithSecret);

            SentryCore.ConfigureScope(p =>
            {
                called = true;
                Assert.Empty(p.Breadcrumbs);
            });
            Assert.True(called);

            first.Dispose();
            second.Dispose();
        }
Example #5
0
        public void Disposable_MultipleCalls_NoOp()
        {
            var disposable = SentryCore.Init();

            disposable.Dispose();
            disposable.Dispose();
            Assert.False(SentryCore.IsEnabled);
        }
Example #6
0
 public void Init_ValidDsnEnvironmentVariable_EnablesSdk()
 {
     EnvironmentVariableGuard.WithVariable(
         Sentry.Internals.Constants.DsnEnvironmentVariable,
         DsnSamples.ValidDsnWithSecret,
         () =>
     {
         SentryCore.Init();
         Assert.True(SentryCore.IsEnabled);
     });
 }
Example #7
0
 public void Init_DisableDsnEnvironmentVariable_DisablesSdk()
 {
     EnvironmentVariableGuard.WithVariable(
         Sentry.Internals.Constants.DsnEnvironmentVariable,
         Sentry.Internals.Constants.DisableSdkDsnValue,
         () =>
     {
         SentryCore.Init();
         Assert.False(SentryCore.IsEnabled);
     });
 }
Example #8
0
 public void Init_DisableDsnEnvironmentVariable_DisablesSdk()
 {
     EnvironmentVariableGuard.WithVariable(
         DsnEnvironmentVariable,
         DisableSdkDsnValue,
         () =>
     {
         using (SentryCore.Init())
             Assert.False(SentryCore.IsEnabled);
     });
 }
Example #9
0
 public void Init_ValidDsnEnvironmentVariable_EnablesSdk()
 {
     EnvironmentVariableGuard.WithVariable(
         DsnEnvironmentVariable,
         ValidDsnWithSecret,
         () =>
     {
         using (SentryCore.Init())
             Assert.True(SentryCore.IsEnabled);
     });
 }
Example #10
0
 public void Init_InvalidDsnEnvironmentVariable_Throws()
 {
     EnvironmentVariableGuard.WithVariable(
         Sentry.Internals.Constants.DsnEnvironmentVariable,
         // If the variable was set, to non empty string but value is broken, better crash than silently disable
         DsnSamples.InvalidDsn,
         () =>
     {
         var ex = Assert.Throws <ArgumentException>(() => SentryCore.Init());
         Assert.Equal("Invalid DSN: A Project Id is required.", ex.Message);
     });
 }
Example #11
0
        static void Main()
        {
            SentryCore.Init("https://[email protected]/id"); // Initialize SDK

            try
            {
                App();
            }
            finally
            {
                SentryCore.CloseAndFlush();
            }
        }
Example #12
0
        public void Dispose_DisposingFirst_DoesntAffectSecond()
        {
            var first  = SentryCore.Init(ValidDsnWithSecret);
            var second = SentryCore.Init(ValidDsnWithSecret);

            SentryCore.AddBreadcrumb("test", "type");
            first.Dispose();
            var called = false;

            SentryCore.ConfigureScope(p =>
            {
                called = true;
                Assert.Single(p.Breadcrumbs);
            });
            Assert.True(called);
            second.Dispose();
        }
        internal SentryLoggerProvider(
            ISentryScopeManager scopeManager,
            SentryLoggingOptions options)
        {
            Debug.Assert(options != null);
            Debug.Assert(scopeManager != null);

            _options = options;

            // SDK is being initialized through this integration
            // Lifetime is owned by this instance:
            if (_options.InitializeSdk)
            {
                _sdk = SentryCore.Init(_options.InitSdk);
            }

            // Creates a scope so that Integration added below can be dropped when the logger is disposed
            _scope = scopeManager.PushScope();

            // TODO: SDK interface not accepting 'Integrations'
            // scopeManager.ConfigureScope(s => s.Sdk.AddIntegration(Constants.IntegrationName));
        }
Example #14
0
        private static async Task Main(string[] args)
        {
            // With the SDK disabled so the callback is never invoked
            await SentryCore.ConfigureScopeAsync(async scope =>
            {
                // This could be any async I/O operation, like a DB query
                await Task.Yield();
                scope.SetExtra("Key", "Value");
            });

            // Enable the SDK
            SentryCore.Init(o =>
            {
                // Modifications to event before it goes out. Could replace the event altogether
                o.BeforeSend = @event =>
                {
                    // Drop an event altogether:
                    if (@event.Tags.ContainsKey("SomeTag"))
                    {
                        return(null);
                    }

                    // Create a totally new event or modify the current one:
                    @event.ServerName = null; // Make sure no ServerName is sent out
                    return(@event);
                };
            });

            await SentryCore.ConfigureScopeAsync(async scope =>
            {
                // This could be any async I/O operation, like a DB query
                await Task.Yield();
                scope.SetExtra("Key", "Value");
            });

            SentryCore.CaptureException(new Exception("Something went wrong."));

            SentryCore.CloseAndFlush();
        }
Example #15
0
 public void EnabledSdk() => _sdk = SentryCore.Init(Constants.ValidDsn);
Example #16
0
 public void Init_ValidDsnWithSecret_EnablesSdk()
 {
     using (SentryCore.Init(ValidDsnWithSecret))
         Assert.True(SentryCore.IsEnabled);
 }
 public void Init_Dispose_NoDsnFound() => SentryCore.Init().Dispose();
Example #18
0
 public void Init_EmptyDsn_DisabledSdk()
 {
     SentryCore.Init(string.Empty);
     Assert.False(SentryCore.IsEnabled);
 }
 public void Init_Dispose_WithDsn() => SentryCore.Init(Constants.ValidDsn).Dispose();
 public void Init_Dispose_DsnEnvVar() => SentryCore.Init(Constants.ValidDsn).Dispose();
Example #21
0
        private static async Task Main(string[] args)
        {
            // When the SDK is disabled, no callback is executed:
            await SentryCore.ConfigureScopeAsync(async scope =>
            {
                // Never executed:
                // This could be any async I/O operation, like a DB query
                await Task.Yield();
                scope.SetExtra("Key", "Value");
            });

            // Enable the SDK
            using (SentryCore.Init(o =>
            {
                // Modifications to event before it goes out. Could replace the event altogether
                o.BeforeSend = @event =>
                {
                    // Drop an event altogether:
                    if (@event.Tags.ContainsKey("SomeTag"))
                    {
                        return(null);
                    }

                    // Create a totally new event or modify the current one:
                    @event.ServerName = null; // Make sure no ServerName is sent out
                    return(@event);
                };

                // Configure the background worker which sends events to sentry:
                o.Worker(w =>
                {
                    // Poll for events every 100ms
                    w.EmptyQueueDelay = TimeSpan.FromMilliseconds(100);
                    // Wait up to 5 seconds before shutdown while there are events to send.
                    w.ShutdownTimeout = TimeSpan.FromSeconds(5);
                });

                o.Http(h =>
                {
                    // Using a proxy:
                    h.Proxy = null; //new WebProxy("https://localhost:3128");
                });
            }))
            {
                await SentryCore.ConfigureScopeAsync(async scope =>
                {
                    // This could be any async I/O operation, like a DB query
                    await Task.Yield();
                    scope.SetExtra("Key", "Value");
                });

                SentryCore.CaptureException(new Exception("Something went wrong."));

                // -------------------------

                // A custom made client, that could be registered with DI,
                // would get disposed by the container on app shutdown

                // Using a different DSN:
                var adminDsn = new Dsn(AdminDsn);
                using (var adminClient = new SentryClient(new SentryOptions {
                    Dsn = adminDsn
                }))
                {
                    // Make believe web framework middleware
                    var middleware = new AdminPartMiddleware(adminClient, null);
                    var request    = new { Path = "/admin" }; // made up request
                    middleware.Invoke(request);
                } // Dispose the client which flushes any queued events

                SentryCore.CaptureException(
                    new Exception("Error outside of the admin section: Goes to the default DSN"));
            }  // On Dispose: SDK closed, events queued are flushed/sent to Sentry
        }
Example #22
0
 public void Init_ValidDsnWithoutSecret_EnablesSdk()
 {
     SentryCore.Init(DsnSamples.ValidDsnWithoutSecret);
     Assert.True(SentryCore.IsEnabled);
 }
Example #23
0
 public void Init_BrokenDsn_Throws()
 {
     Assert.Throws <UriFormatException>(() => SentryCore.Init("invalid stuff"));
 }