Beispiel #1
0
        public void Empty_configuration_builder_should_be_valid()
        {
            var builder = new DbLoggingConfigurationBuilder();

            builder.OpenConnectionMessage.Should().NotBeNullOrEmpty();
            builder.CloseConnectionMessage.Should().NotBeNullOrEmpty();
            builder.ExecuteQueryMessage.Should().NotBeNullOrEmpty();
            builder.LogLevel.Should().NotBeNull();
            builder.LogSensitiveData.Should().HaveValue();
            builder.ConnectionProjector.Should().NotBeNull();
        }
        /// <summary>
        /// Registers the LogDbConnectionFactory in the IoC container 
        /// </summary>
        /// <param name="services">The services collection</param>
        /// <param name="factory">The connection factory delegate</param>
        /// <param name="config">The configuration delegate</param>
        /// <param name="lifetime">The service lifetime</param>
        /// <returns></returns>
        public static IServiceCollection AddDbConnectionFactory(
            this IServiceCollection services, 
            Func<IServiceProvider, DbConnection> factory,
            Action<DbLoggingConfigurationBuilder> config = null,
            ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            var builder = new DbLoggingConfigurationBuilder();
            config?.Invoke(builder);

            object FactoryWrapper(IServiceProvider x) => new LogDbConnectionFactory(
                x.GetService<ILogger<DbConnection>>(), 
                () => factory(x),
                builder.Build());

            services.TryAdd(new ServiceDescriptor(typeof(IDbConnectionFactory), FactoryWrapper, lifetime));
            return services;
        }
Beispiel #3
0
        /// <summary>
        /// Registers DbConnectionFactory (with context logging) in the IoC container
        /// </summary>
        /// <param name="services">The services collection</param>
        /// <param name="factory">The connection factory delegate</param>
        /// <param name="config">The configuration delegate</param>
        /// <param name="lifetime">The service lifetime</param>
        /// <returns></returns>
        public static IServiceCollection AddDbConnectionFactoryWithCtx <T>(
            this IServiceCollection services,
            Func <IServiceProvider, DbConnection> factory,
            Func <DbLoggingConfigurationBuilder, DbLoggingConfigurationBuilder> config = null,
            ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            var builder = new DbLoggingConfigurationBuilder();

            builder = config?.Invoke(builder) ?? builder;

            object FactoryWrapper(IServiceProvider x) =>
            new ContextfulLoggingFactory <T>(
                x.GetService <ILogger <IDbConnectionFactory <T> > >(),
                builder.Build(),
                () => factory(x));

            services.TryAdd(new ServiceDescriptor(typeof(IDbConnectionFactory <T>), FactoryWrapper, lifetime));
            return(services);
        }
Beispiel #4
0
        public void Configuration_builder_full_of_nulls_should_be_valid()
        {
            var builder = new DbLoggingConfigurationBuilder
            {
                ConnectionProjector    = null,
                LogLevel               = null,
                CloseConnectionMessage = null,
                ExecuteQueryMessage    = null,
                LogSensitiveData       = null,
                OpenConnectionMessage  = null
            };
            var cfg = builder.Build();

            cfg.OpenConnectionMessage.Should().NotBeNullOrEmpty();
            cfg.CloseConnectionMessage.Should().NotBeNullOrEmpty();
            cfg.ExecuteQueryMessage.Should().NotBeNullOrEmpty();
            cfg.LogLevel.Should().Be(LogLevel.Information);
            cfg.LogSensitiveData.Should().BeFalse();
            cfg.ConnectionProjector.Should().NotBeNull();
        }