/// <summary> /// Adds Sentry's services to the <see cref="IServiceCollection"/> /// </summary> /// <param name="services">The services.</param> /// <returns></returns> public static IServiceCollection AddSentry <TOptions>(this IServiceCollection services) where TOptions : SentryLoggingOptions, new() { services.TryAddSingleton <SentryOptions>( c => c.GetRequiredService <IOptions <TOptions> >().Value); services.TryAddTransient <ISentryClient>(c => c.GetRequiredService <IHub>()); services.TryAddTransient(c => c.GetRequiredService <Func <IHub> >()()); services.TryAddSingleton <Func <IHub> >(c => { var options = c.GetRequiredService <IOptions <TOptions> >().Value; if (options.InitializeSdk) { var hub = SentrySdk.InitHub(options); SentrySdk.UseHub(hub); } return(() => HubAdapter.Instance); }); // Custom handler for HttpClientFactory. // Must be singleton: https://github.com/getsentry/sentry-dotnet/issues/785 services.TryAddSingleton <IHttpMessageHandlerBuilderFilter, SentryHttpMessageHandlerBuilderFilter>(); return(services); }
/// <summary> /// Adds the Sentry logging integration. /// </summary> /// <remarks> /// This method does not need to be called when calling `UseSentry` with ASP.NET Core /// since that integrates with the logging framework automatically. /// </remarks> /// <param name="factory">The factory.</param> /// <param name="optionsConfiguration">The options configuration.</param> /// <returns></returns> public static ILoggerFactory AddSentry( this ILoggerFactory factory, Action <SentryLoggingOptions> optionsConfiguration = null) { var options = new SentryLoggingOptions(); optionsConfiguration?.Invoke(options); if (options.DiagnosticLogger == null) { var logger = factory.CreateLogger <ISentryClient>(); options.DiagnosticLogger = new MelDiagnosticLogger(logger, options.DiagnosticsLevel); } IHub hub; if (options.InitializeSdk) { hub = new OptionalHub(options); SentrySdk.UseHub(hub); } else { // Access to whatever the SentrySdk points to (disabled or initialized via SentrySdk.Init) hub = HubAdapter.Instance; } factory.AddProvider(new SentryLoggerProvider(hub, SystemClock.Clock, options)); return(factory); }
public void FinishSentryTransaction_FinishesTransaction() { // Arrange using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub( new SentryOptions { Dsn = "https://[email protected]:65535/2147483647" }, Substitute.For <ISentryClient>() )); var context = new HttpContext( new HttpRequest("foo", "https://localhost/person/13", "details=true") { RequestType = "GET" }, new HttpResponse(TextWriter.Null) { StatusCode = 404 } ); // Act var transaction = context.StartSentryTransaction(); context.FinishSentryTransaction(); // Assert transaction.IsFinished.Should().BeTrue(); transaction.Status.Should().Be(SpanStatus.NotFound); }
/// <summary> /// Adds Sentry's services to the <see cref="IServiceCollection"/> /// </summary> /// <param name="services">The services.</param> /// <returns></returns> public static IServiceCollection AddSentry <TOptions>(this IServiceCollection services) where TOptions : SentryLoggingOptions, new() { services.TryAddSingleton <SentryOptions>( c => c.GetRequiredService <IOptions <TOptions> >().Value); services.TryAddSingleton <OptionalHub>(); services.TryAddTransient <ISentryClient>(c => c.GetRequiredService <IHub>()); services.TryAddTransient(c => c.GetRequiredService <Func <IHub> >()()); services.TryAddSingleton <Func <IHub> >(c => { var options = c.GetRequiredService <IOptions <TOptions> >().Value; if (options.InitializeSdk) { var hub = c.GetRequiredService <OptionalHub>(); _ = SentrySdk.UseHub(hub); } return(() => HubAdapter.Instance); }); return(services); }
public void StartSentryTransaction_BindsToScope() { // Arrange using var _ = SentrySdk.UseHub(new Hub( new SentryOptions { Dsn = "https://[email protected]:65535/2147483647" }, Substitute.For <ISentryClient>() )); var context = HttpContextBuilder.Build(); // Act var transaction = context.StartSentryTransaction(); var transactionFromScope = SentrySdk.GetSpan(); // Assert transactionFromScope.Should().BeSameAs(transaction); }
public OptionalHub(SentryOptions options) { options.SetupLogging(); if (options.Dsn == null) { if (!Dsn.TryParse(DsnLocator.FindDsnStringOrDisable(), out var dsn)) { options.DiagnosticLogger?.LogWarning("Init was called but no DSN was provided nor located. Sentry SDK will be disabled."); _hub = HubAdapter.Instance; return; } options.Dsn = dsn; } _hub = new Hub(options); _disposable = SentrySdk.UseHub(_hub); }
public void AddSentry_ConfigureScope_InvokesCallback() { const SentryLevel expected = SentryLevel.Debug; var sut = Substitute.For <ILoggerFactory>(); var hub = Substitute.For <IHub>(); var scope = new Scope(new SentryOptions()); hub.When(w => w.ConfigureScope(Arg.Any <Action <Scope> >())) .Do(info => info.Arg <Action <Scope> >()(scope)); SentrySdk.UseHub(hub); sut.AddSentry(o => { o.InitializeSdk = false; // use the mock above o.ConfigureScope(s => s.Level = expected); }); Assert.Equal(expected, scope.Level); }
/// <summary> /// Adds the Sentry logging integration. /// </summary> /// <remarks> /// This method does not need to be called when calling `UseSentry` with ASP.NET Core /// since that integrates with the logging framework automatically. /// </remarks> /// <param name="factory">The factory.</param> /// <param name="optionsConfiguration">The options configuration.</param> /// <returns></returns> public static ILoggerFactory AddSentry( this ILoggerFactory factory, Action <SentryLoggingOptions>?optionsConfiguration = null) { var options = new SentryLoggingOptions(); optionsConfiguration?.Invoke(options); if (options.DiagnosticLogger == null) { var logger = factory.CreateLogger <ISentryClient>(); options.DiagnosticLogger = new MelDiagnosticLogger(logger, options.DiagnosticsLevel); } IHub hub; if (options.InitializeSdk) { if (SentrySdk.IsEnabled && options.Dsn is null) { options.DiagnosticLogger?.LogWarning("Not calling Init from {0} because SDK is already enabled and no DSN was provided to the integration", nameof(SentryLoggerFactoryExtensions)); hub = HubAdapter.Instance; } else { options.DiagnosticLogger?.LogDebug("Initializing from {0} and swapping current Hub.", nameof(SentryLoggerFactoryExtensions)); hub = OptionalHub.FromOptions(options); _ = SentrySdk.UseHub(hub); } } else { // Access to whatever the SentrySdk points to (disabled or initialized via SentrySdk.Init) hub = HubAdapter.Instance; } factory.AddProvider(new SentryLoggerProvider(hub, SystemClock.Clock, options)); return(factory); }
public void StartSentryTransaction_BindsToScope() { // Arrange using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub( new SentryOptions { Dsn = "https://[email protected]:65535/2147483647" }, Substitute.For <ISentryClient>() )); var context = new HttpContext( new HttpRequest("foo", "https://localhost/person/13", "details=true") { RequestType = "GET" }, new HttpResponse(TextWriter.Null)); // Act var transaction = context.StartSentryTransaction(); var transactionFromScope = SentrySdk.GetSpan(); // Assert transactionFromScope.Should().BeSameAs(transaction); }
public void FinishSentryTransaction_FinishesTransaction() { // Arrange using var _ = SentrySdk.UseHub(new Hub( new SentryOptions { Dsn = "https://[email protected]:65535/2147483647" }, Substitute.For <ISentryClient>() )); var context = HttpContextBuilder.Build(404); // Act var transaction = context.StartSentryTransaction(); context.FinishSentryTransaction(); // Assert transaction.IsFinished.Should().BeTrue(); transaction.Status.Should().Be(SpanStatus.NotFound); }
public HubAdapterTests() { Hub = Substitute.For <IHub>(); _ = SentrySdk.UseHub(Hub); }