/// <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); }
/// <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); }
/// <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 = OptionalHub.FromOptions(options); _ = SentrySdk.UseHub(hub); } return(() => HubAdapter.Instance); }); return(services); }
public void Ctor_NoDsn_DisposeDoesNotThrow() { var sut = new OptionalHub(new SentryOptions()); sut.Dispose(); }
public async Task FromOptions_NoDsn_FlushAsyncDoesNotThrow() { var sut = OptionalHub.FromOptions(new SentryOptions()); await sut.FlushAsync(TimeSpan.FromDays(1)); }
public void FromOptions_NoDsn_DisposeDoesNotThrow() { var sut = OptionalHub.FromOptions(new SentryOptions()) as IDisposable; sut?.Dispose(); }
public Task Ctor_NoDsn_FlushAsyncDoesNotThrow() { var sut = new OptionalHub(new SentryOptions()); return(sut.FlushAsync(TimeSpan.FromDays(1))); }