public ISqlSugarClient Db
{
get
{
//sql执行前
//_currentDb.Aop.OnLogExecuting = (sql, pars) =>
//{
// //string n1 = UtilMethods.GetNativeSql(sql, pars);//日志使用
// string n2 = UtilMethods.GetSqlString(DbType.Vastbase, sql, pars);//调试使用
// LogTextHelper.WriteLog_SqlLog("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + n2 + ";【sql执行前】耗时:" + _currentDb.Ado.SqlExecutionTime.TotalMilliseconds + "ms");
//};
////sql执行完
_currentDb.Aop.OnLogExecuted = (sql, pars) =>
{
if (_currentDb.Ado.SqlExecutionTime.TotalMilliseconds > 7 && !_currentDb.MappingTables.Any(c => c.EntityName.Contains("System")))//sql执行耗时大于20ms记录日志
{
//string n1 = UtilMethods.GetNativeSql(sql, pars);//日志使用
string n2 = UtilMethods.GetSqlString(DbType.Vastbase, sql, pars);//调试使用
LogTextHelper.WriteLog_SqlLog("sql执行完时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + n2 + ";【sql执行后】耗时:" + _currentDb.Ado.SqlExecutionTime.TotalMilliseconds + "ms");
}
};
//执行sql 错误事件
_currentDb.Aop.OnError = (exp) =>
{
var json = JsonConvert.SerializeObject(exp.Parametres);
// 解析JSON数据
JArray jsonArray = JArray.Parse(json);
string sql = exp.Sql;
// 遍历JSON数组
foreach (JObject jsonObject in jsonArray)
{
// 获取ParameterName和Value
string parameterName = jsonObject["ParameterName"]?.ToString();
string value = jsonObject["Value"]?.ToString();
// 创建一个正则表达式,确保只匹配完整的单词,并且以 @开头后跟字母
string pattern = @"@([a-zA-Z_][a-zA-Z0-9_]*)";
// 使用正则表达式替换字符串
sql = Regex.Replace(sql, pattern, m =>
{
if (m.Value == parameterName)
{
return "'" + value + "'";
}
return m.Value;
});
}
LogTextHelper.WriteLog_SqlLog("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "【错误】" + "\r\n【异常Sql】:" + sql);
};
return _currentDb;
}
}
标签:string,currentDb,AOP,Value,pars,sql,ToString,SqlSugar From: https://www.cnblogs.com/9874lingxi/p/18551997