Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
    /// <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>
    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.DiagnosticLevel);
        }

        IHub hub;

        if (options.InitializeSdk)
        {
            if (SentrySdk.IsEnabled && options.Dsn is null)
            {
                options.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.LogDebug("Initializing from {0} and swapping current Hub.", nameof(SentryLoggerFactoryExtensions));
                hub = SentrySdk.InitHub(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);
    }
Beispiel #3
0
        /// <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);
            });

            return(services);
        }