예제 #1
0
        public static void AddSqlSugarSevice(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            services.AddScoped <ISqlSugarClient>(o =>
            {
                // 连接字符串
                var listConfig = new List <ConnectionConfig>
                {
                    new ConnectionConfig
                    {
                        ConfigId              = AppConfigurtaionService.Configuration["ConnectionStrings:DefaultDBConfigId"], // 此链接标志,用以后面切库使用
                        ConnectionString      = AppConfigurtaionService.Configuration["ConnectionStrings:Default"],           // 业务库连接字符串
                        DbType                = DbType.SqlServer,
                        IsShardSameThread     = true,
                        IsAutoCloseConnection = true,          // 开启自动释放模式和EF原理一样我就不多解释了
                        // InitKeyType = InitKeyType.Attribute,// 从特性读取主键和自增列信息
                        InitKeyType = InitKeyType.SystemTable, // 从数据库读取主键和自增列信息

                        AopEvents = new AopEvents
                        {
                            OnLogExecuting = async(sql, pars) =>
                            {
                                // MiniProfiler.Current.CustomTiming("SQL:", GetParas(pars) + "【SQL语句】:" + sql);
                                if (AppConfigurtaionService.Configuration["ConnectionStrings:DbMSSQLMainIsLogSql"] == "True")// 是否记录Sql语句到日志文件中的开关
                                {
                                    log.Info(GetParas(pars) + "【SQL语句】:" + sql);
                                }
                                if (AppConfigurtaionService.Configuration["ConnectionStrings:DbMSSQLMainIsLogSqlToLog"] == "True")// 是否记录Sql语句到数据库中的开关
                                {
                                    // 添加Sql语句到日志库
                                    await LogDB.Insertable <HW_SqlLog>(
                                        new CreateSqlLogDto()
                                    {
                                        Sql = GetParas(pars) + "【SQL语句】:" + sql
                                    })
                                    .ExecuteReturnBigIdentityAsync();
                                }
                            }
                        }
                    },
                    new ConnectionConfig
                    {
                        ConfigId              = AppConfigurtaionService.Configuration["ConnectionStrings:DefaultLogDBConfigId"], // 此链接标志,用以后面切库使用
                        ConnectionString      = AppConfigurtaionService.Configuration["ConnectionStrings:DefaultLog"],           // 日志库连接字符串
                        DbType                = DbType.SqlServer,
                        IsShardSameThread     = true,
                        IsAutoCloseConnection = true,          // 开启自动释放模式和EF原理一样我就不多解释了
                        // InitKeyType = InitKeyType.Attribute,// 从特性读取主键和自增列信息
                        InitKeyType = InitKeyType.SystemTable, // 从数据库读取主键和自增列信息

                        AopEvents = new AopEvents
                        {
                            OnLogExecuting = (sql, pars) =>
                            {
                                // MiniProfiler.Current.CustomTiming("SQL:", GetParas(pars) + "【SQL语句】:" + sql);
                                if (AppConfigurtaionService.Configuration["ConnectionStrings:DbMSSQLLogIsLogSql"] == "True")// 是否记录Sql语句到日志文件中的开关
                                {
                                    log.Info(GetParas(pars) + "【SQL语句】:" + sql);
                                }
                            }
                        }
                    }
                };
                return(new SqlSugarClient(listConfig));
            });
        }