切换数据库后AOP日志不再输出 返回

C#论坛 老数据
6 1661

 Db = new SqlSugarClient(new List<ConnectionConfig>(){

                 new ConnectionConfig(){ ConfigId="1", DbType=DbType.MySql,

                ConnectionString=GetConfig.MainDbConfig,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },

                new ConnectionConfig(){ ConfigId="2", DbType=DbType.MySql,

                ConnectionString=GetConfig.MainDb2Config ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}

            } );

默认是1数据库,能打印AOP日志,切换成2数据库后db.ChangeDatabase("2");, Db.Aop.OnLogExecuting = (sql, pars)  不再打印2数据库的日志,再切换回默认的1数据库AOP日志还是能打印输出

热忱回答6

  • 要统一你可以这样写      

    SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
    {
       new ConnectionConfig(){ ConfigId="1",AopEvents=AopEvents, DbType=DbType.SqlServer, 
       ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
       
       new ConnectionConfig(){ ConfigId="2",AopEvents=AopEvents,DbType=DbType.SqlServer, 
       ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
    });


    0 回复
  • @fate stay night:我本来就是这样写的,我是说日志,切换成2数据库后,日志就不会输出,只有切换回1才会正常输出日志

    0 回复
  • @fate stay night

    public class DbContext<T> where T : class, new()

        {


            public SqlSugarClient Db;

            public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }

            public DbContext()

            {

                Db = new SqlSugarClient(new List<ConnectionConfig>(){

                     new ConnectionConfig(){ ConfigId="1", DbType=DbType.MySql,

                    ConnectionString=GetConfig.MainDbConfig,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },

                    new ConnectionConfig(){ ConfigId="2", DbType=DbType.MySql,

                    ConnectionString=GetConfig.MainDb2Config ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}

                } );

                //调式代码 用来打印SQL 

                Db.Aop.OnLogExecuting = (sql, pars) =>

                {

                    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    System.Diagnostics.Debug.WriteLine(sql);

                    System.Diagnostics.Debug.WriteLine(Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));

                };



            }


        }


    public ActionResult Index()

    {

       //这里AOP监控到了,有输出SQL

     DbContext<test1> dbContext = new Utils.DbContext<test1>();

                var c = dbContext.Db.Queryable<test1>()

                    .Where(it => it.id == 1)

                    .ToJson();

           

    //这里切换成2数据库,AOP没有监控到,没有输出SQL

                dbContext.Db.ChangeDatabase("2");

                var a= dbContext.Db.Queryable<test1>()

                     .AS("t1")

                      .ToJson()

                    ;

           //这里再切换到1数据库,AOP有监控到,输出了SQL

                dbContext.Db.ChangeDatabase("1");

                var b = dbContext.Db.Queryable<test1>()

                    .ToJson() ;

     }         

    )


    我的问题就是这个切换2数据库后,没监控到AOP日志输出

    0 回复

  • SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()

    {

       new ConnectionConfig(){ ConfigId="1",AopEvents=AopEvents, DbType=DbType.SqlServer, 

       ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },

        

       new ConnectionConfig(){ ConfigId="2",AopEvents=AopEvents,DbType=DbType.SqlServer, 

       ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}

    });


    0 回复
  • 用同一个aopevents就行了

    0 回复
  • @fate stay night:恩试了可以

    0 回复