浏览代码

控制台打印sql语句调整为只打印错误语句

tian_z 3 年之前
父节点
当前提交
0b9631b52f
共有 1 个文件被更改,包括 14 次插入7 次删除
  1. 14 7
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

+ 14 - 7
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -76,16 +76,23 @@ namespace Admin.NET.Core
                         // 打印SQL语句
                         dbProvider.Aop.OnLogExecuting = (sql, pars) =>
                         {
-                            if (sql.StartsWith("SELECT"))
-                                Console.ForegroundColor = ConsoleColor.Green;
-                            if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
-                                Console.ForegroundColor = ConsoleColor.White;
-                            if (sql.StartsWith("DELETE"))
-                                Console.ForegroundColor = ConsoleColor.Blue;
+                            //if (sql.StartsWith("SELECT"))
+                            //    Console.ForegroundColor = ConsoleColor.Green;
+                            //if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
+                            //    Console.ForegroundColor = ConsoleColor.White;
+                            //if (sql.StartsWith("DELETE"))
+                            //    Console.ForegroundColor = ConsoleColor.Blue;
 
-                            Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
+                            //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                             App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                         };
+                        dbProvider.Aop.OnError = (ex) =>
+                        {
+                            Console.ForegroundColor = ConsoleColor.Red;
+                            var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
+                            Console.WriteLine($"{ex.Message}{Environment.NewLine}{ex.Sql}{Environment.NewLine}{pars}{Environment.NewLine}");
+                            App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
+                        };
 
                         // 数据审计
                         dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>