From c7e6e322bcc3809ba8504e71111d1c4dff627e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Thu, 7 Dec 2023 11:33:19 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E6=96=B0=E5=A2=9E=E6=89=A7=E8=A1=8Cs?= =?UTF-8?q?ql=E8=B6=85=E6=97=B6=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/appsettings.json | 1 + ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 665e190..f91383c 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -39,6 +39,7 @@ "DemoMode": false, //是否演示模式 "SingleLogin": false, //是否允许多设备/浏览器登录 "workId": 1, //雪花id唯一数字 + "sqlExecutionTime": 5,//Sql执行时间超过多少秒记录日志并警报 "Upload": { "uploadUrl": "http://localhost:8888", //本地存储资源访问路径 "localSavePath": "", //本地上传默认文件存储目录 wwwroot diff --git a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs index cc45692..1f08e62 100644 --- a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs +++ b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SqlSugar.IOC; +using ZR.Common; using ZR.Model.System; namespace ZR.ServiceCore.SqlSugar @@ -202,6 +203,22 @@ namespace ZR.ServiceCore.SqlSugar #endregion } }; + db.GetConnectionScope(configId).Aop.OnLogExecuted = (sql, pars) => + { + var sqlExecutionTime = AppSettings.Get("sqlExecutionTime"); + if (db.Ado.SqlExecutionTime.TotalSeconds > sqlExecutionTime) + { + //代码CS文件名 + var fileName = db.Ado.SqlStackTrace.FirstFileName; + //代码行数 + var fileLine = db.Ado.SqlStackTrace.FirstLine; + //方法名 + var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName; + var logInfo = $"Sql执行超时,用时{db.Ado.SqlExecutionTime.TotalSeconds}秒【{sql}】,fileName={fileName},line={fileLine},methodName={FirstMethodName}"; + WxNoticeHelper.SendMsg("Sql请求时间过长",logInfo); + logger.Warn(logInfo); + } + }; } private static object GetParsValue(SugarParameter x)