コード例 #1
0
        /// <summary>
        /// Adds services for middleware that will report all uncaught exceptions to the
        /// Stackdriver Error Reporting API.
        /// <para>
        /// Can be used when running on Google App Engine or Google Compute Engine.
        /// The Google Cloud Platform project to report errors to will detected from the
        /// current platform.
        /// </para>
        /// </summary>
        /// <param name="services">The service collection. Must not be null.</param>
        /// <param name="setupAction">Action to set up options. Must not be null.</param>
        /// <remarks>
        /// If <see cref="RetryOptions.ExceptionHandling"/> is set to <see cref="ExceptionHandling.Propagate"/>
        /// and the <see cref="RequestDelegate"/> executed by this middleware throws an exception and this
        /// diagnostics library also throws an exception trying to report it an <see cref="AggregateException"/>
        /// with both exceptions will be thrown.  Otherwise only the exception from the <see cref="RequestDelegate"/>
        /// will be thrown.
        /// </remarks>
        public static IServiceCollection AddGoogleExceptionLogging(
            this IServiceCollection services, Action <ErrorReportingServiceOptions> setupAction)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));
            GaxPreconditions.CheckNotNull(setupAction, nameof(setupAction));

            var serviceOptions = new ErrorReportingServiceOptions();

            setupAction(serviceOptions);
            services.AddHttpContextAccessor();
            services.AddSingleton(ContextExceptionLogger.Create(
                                      serviceOptions.ProjectId, serviceOptions.ServiceName, serviceOptions.Version, serviceOptions.Options));
            return(services.AddSingleton(CreateExceptionLogger));
        }
        /// <summary>
        /// Adds services for middleware that will report all uncaught exceptions to the
        /// Stackdriver Error Reporting API.
        /// <para>
        /// Can be used when running on Google App Engine or Google Compute Engine.
        /// The Google Cloud Platform project to report errors to will detected from the
        /// current platform.
        /// </para>
        /// </summary>
        /// <param name="services">The service collection. Must not be null.</param>
        /// <param name="setupAction">Action to set up options. Must not be null.</param>
        /// <remarks>
        /// If <see cref="RetryOptions.ExceptionHandling"/> is set to <see cref="ExceptionHandling.Propagate"/>
        /// and the <see cref="RequestDelegate"/> executed by this middleware throws an exception and this
        /// diagnostics library also throws an exception trying to report it an <see cref="AggregateException"/>
        /// with both exceptions will be thrown.  Otherwise only the exception from the <see cref="RequestDelegate"/>
        /// will be thrown.
        /// </remarks>
        public static IServiceCollection AddGoogleExceptionLogging(
            this IServiceCollection services, Action <ErrorReportingServiceOptions> setupAction)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));
            GaxPreconditions.CheckNotNull(setupAction, nameof(setupAction));

            var serviceOptions = new ErrorReportingServiceOptions();

            setupAction(serviceOptions);
            var serviceName = GaxPreconditions.CheckNotNull(serviceOptions.ServiceName, nameof(serviceOptions.ServiceName));
            var version     = GaxPreconditions.CheckNotNull(serviceOptions.Version, nameof(serviceOptions.Version));

            // Only add the HttpContextAccessor if it's not already added.
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton(ContextExceptionLogger.Create(
                                      serviceOptions.ProjectId, serviceName, version, serviceOptions.Options));
            return(services.AddSingleton(CreateExceptionLogger));
        }