Example #1
0
        public static void UseNlog(this IApplicationBuilder app)
        {
            LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();
            LogManager.Configuration.Variables["configDir"] = Appsettings.app("AppSettings", "LogFilesDir");
            var connectionString =
                DesEncryptUtil.Decrypt(Appsettings.app("ConnectionStrings", "MySqlConnectionString"));

            LogManager.Configuration.Variables["connectionString"] = connectionString;
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); //避免日志中的中文输出乱码
        }
        /// <summary>
        /// 核心代码,获取连接实例
        /// 通过双if 夹lock的方式,实现单例模式
        /// </summary>
        /// <returns></returns>
        private ConnectionMultiplexer GetConnection()
        {
            //如果已经连接实例,直接返回
            if (_connection != null && _connection.IsConnected)
            {
                return(_connection);
            }

            //加锁,防止异步编程中,出现单例无效的问题
            lock (_redisLock)
            {
                //释放redis连接
                _connection?.Dispose();

                try
                {
                    var config = new ConfigurationOptions
                    {
                        AbortOnConnectFail = false,
                        AllowAdmin         = true,
                        ConnectTimeout     = 15000, //改成15s
                        SyncTimeout        = 5000,
                        EndPoints          = { _connenctionString } // connectionString 为IP:Port 如”192.168.2.110:6379”
                    };
                    if (!string.IsNullOrEmpty(_connenctionPwd))
                    {
                        config.Password = DesEncryptUtil.Decrypt(_connenctionPwd); //Redis数据库密码;
                    }
                    _connection = ConnectionMultiplexer.Connect(config);
                }
                catch (Exception)
                {
                    throw new Exception("Redis服务未启用,请开启该服务,并且请注意端口号");
                }
            }

            return(_connection);
        }
Example #3
0
        public static void AddFreeSql(this IServiceCollection services)
        {
            IFreeSql fsql = new FreeSqlBuilder()
                            .UseConnectionString(DataType.MySql,
                                                 DesEncryptUtil.Decrypt(Appsettings.app("ConnectionStrings", "MySqlConnectionString")))
                            .UseNoneCommandParameter(true)
                            .UseMonitorCommand(cmd => { Trace.WriteLine(cmd.CommandText + ";"); })
                            .Build();

            fsql.Aop.CurdAfter += (s, e) =>
            {
                Console.WriteLine(e.Sql);
                LogHelper.Info(e.Sql);

                if (e.ElapsedMilliseconds > 200)
                {
                    LogHelper.Warning("Sql执行超时,请注意查看");
                }
            };

            services.AddSingleton(fsql);
            services.AddScoped <UnitOfWorkManager>();
            services.AddFreeRepository(filter => filter.Apply <ISoftDeleteEntity>("IsDelete", a => a.IsDelete == false));
        }