11using Microsoft . EntityFrameworkCore ;
22using Microsoft . EntityFrameworkCore . Internal ;
33using Microsoft . Extensions . DependencyInjection . Extensions ;
4- using Netcorext . Contracts ;
54using Netcorext . EntityFramework . UserIdentityPattern . Interceptors ;
65
76namespace Netcorext . EntityFramework . UserIdentityPattern . AspNetCore ;
87
98public static class ServiceCollectionExtension
109{
11- private const long DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD = 1000 ;
10+ private const long DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD = 100 ;
11+ private const long DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD = 100 ;
1212
13- public static IServiceCollection AddIdentityDbContext ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
14- => AddIdentityDbContext < IdentityReplicaDbContext < MasterContext > > ( services , null , lifetime , slowCommandLoggingThreshold ) ;
13+ public static IServiceCollection AddIdentityDbContext ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
14+ => AddIdentityDbContext < IdentityReplicaDbContext < MasterContext > > ( services , null , lifetime , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
1515
16- public static IServiceCollection AddIdentityDbContext ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
17- => AddIdentityDbContext < IdentityReplicaDbContext < MasterContext > > ( services , optionsAction , lifetime , slowCommandLoggingThreshold ) ;
16+ public static IServiceCollection AddIdentityDbContext ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
17+ => AddIdentityDbContext < IdentityReplicaDbContext < MasterContext > > ( services , optionsAction , lifetime , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
1818
19- public static IServiceCollection AddIdentitySlaveDbContext ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
20- => AddIdentityDbContext < IdentityReplicaDbContext < SlaveContext > > ( services , null , lifetime , slowCommandLoggingThreshold ) ;
19+ public static IServiceCollection AddIdentitySlaveDbContext ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
20+ => AddIdentityDbContext < IdentityReplicaDbContext < SlaveContext > > ( services , null , lifetime , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
2121
22- public static IServiceCollection AddIdentitySlaveDbContext ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
23- => AddIdentityDbContext < IdentityReplicaDbContext < SlaveContext > > ( services , optionsAction , lifetime , slowCommandLoggingThreshold ) ;
22+ public static IServiceCollection AddIdentitySlaveDbContext ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
23+ => AddIdentityDbContext < IdentityReplicaDbContext < SlaveContext > > ( services , optionsAction , lifetime , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
2424
25- public static IServiceCollection AddIdentityDbContext < TContext > ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
25+ public static IServiceCollection AddIdentityDbContext < TContext > ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
2626 where TContext : DatabaseContext =>
27- AddIdentityDbContext < TContext > ( services , null , lifetime , slowCommandLoggingThreshold ) ;
27+ AddIdentityDbContext < TContext > ( services , null , lifetime , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
2828
29- public static IServiceCollection AddIdentityDbContext < TContext > ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
29+ public static IServiceCollection AddIdentityDbContext < TContext > ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , ServiceLifetime lifetime = ServiceLifetime . Scoped , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
3030 where TContext : DatabaseContext
3131 {
3232 services . AddContextState ( ) ;
3333
3434 services . AddDbContext < TContext > ( ( provider , builder ) =>
3535 {
3636 var loggerFactory = provider . GetRequiredService < ILoggerFactory > ( ) ;
37- builder . AddInterceptors ( new SlowCommandLoggingInterceptor ( loggerFactory , slowCommandLoggingThreshold ) ) ;
37+
38+ builder . AddInterceptors (
39+ new SlowConnectionLoggingInterceptor ( loggerFactory , slowConnectionLoggingThreshold ) ,
40+ new SlowCommandLoggingInterceptor ( loggerFactory , slowCommandLoggingThreshold )
41+ ) ;
42+
3843 optionsAction ? . Invoke ( provider , builder ) ;
3944 } , lifetime ) ;
4045
@@ -47,31 +52,36 @@ public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCol
4752 return services ;
4853 }
4954
50- public static IServiceCollection AddIdentityDbContextPool ( this IServiceCollection services , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
51- => AddIdentityDbContextPool < IdentityReplicaDbContext < MasterContext > > ( services , null , poolSize , slowCommandLoggingThreshold ) ;
55+ public static IServiceCollection AddIdentityDbContextPool ( this IServiceCollection services , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
56+ => AddIdentityDbContextPool < IdentityReplicaDbContext < MasterContext > > ( services , null , poolSize , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
5257
53- public static IServiceCollection AddIdentityDbContextPool ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
54- => AddIdentityDbContextPool < IdentityReplicaDbContext < MasterContext > > ( services , optionsAction , poolSize , slowCommandLoggingThreshold ) ;
58+ public static IServiceCollection AddIdentityDbContextPool ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
59+ => AddIdentityDbContextPool < IdentityReplicaDbContext < MasterContext > > ( services , optionsAction , poolSize , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
5560
56- public static IServiceCollection AddIdentitySlaveDbContextPool ( this IServiceCollection services , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
57- => AddIdentityDbContextPool < IdentityReplicaDbContext < SlaveContext > > ( services , null , poolSize , slowCommandLoggingThreshold ) ;
61+ public static IServiceCollection AddIdentitySlaveDbContextPool ( this IServiceCollection services , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
62+ => AddIdentityDbContextPool < IdentityReplicaDbContext < SlaveContext > > ( services , null , poolSize , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
5863
59- public static IServiceCollection AddIdentitySlaveDbContextPool ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
60- => AddIdentityDbContextPool < IdentityReplicaDbContext < SlaveContext > > ( services , optionsAction , poolSize , slowCommandLoggingThreshold ) ;
64+ public static IServiceCollection AddIdentitySlaveDbContextPool ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
65+ => AddIdentityDbContextPool < IdentityReplicaDbContext < SlaveContext > > ( services , optionsAction , poolSize , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
6166
62- public static IServiceCollection AddIdentityDbContextPool < TContext > ( this IServiceCollection services , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
67+ public static IServiceCollection AddIdentityDbContextPool < TContext > ( this IServiceCollection services , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
6368 where TContext : DatabaseContext =>
64- AddIdentityDbContextPool < TContext > ( services , null , poolSize , slowCommandLoggingThreshold ) ;
69+ AddIdentityDbContextPool < TContext > ( services , null , poolSize , slowConnectionLoggingThreshold , slowCommandLoggingThreshold ) ;
6570
66- public static IServiceCollection AddIdentityDbContextPool < TContext > ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
71+ public static IServiceCollection AddIdentityDbContextPool < TContext > ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction , int poolSize = 1024 , long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD , long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD )
6772 where TContext : DatabaseContext
6873 {
6974 services . AddContextState ( ) ;
7075
7176 services . AddDbContextPool < TContext > ( ( provider , builder ) =>
7277 {
7378 var loggerFactory = provider . GetRequiredService < ILoggerFactory > ( ) ;
74- builder . AddInterceptors ( new SlowCommandLoggingInterceptor ( loggerFactory , slowCommandLoggingThreshold ) ) ;
79+
80+ builder . AddInterceptors (
81+ new SlowConnectionLoggingInterceptor ( loggerFactory , slowConnectionLoggingThreshold ) ,
82+ new SlowCommandLoggingInterceptor ( loggerFactory , slowCommandLoggingThreshold )
83+ ) ;
84+
7585 optionsAction ? . Invoke ( provider , builder ) ;
7686 } , poolSize ) ;
7787
0 commit comments