159 lines
5.8 KiB
C#
159 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using RIZO_Application.Infrastructure.Model;
|
|
using SqlSugar;
|
|
using SqlSugar.IOC;
|
|
|
|
namespace RIZO_Application.Infrastructure.SqlSugarConfig
|
|
{
|
|
public static class SqlsugarSetup
|
|
{
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// 初始化db
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="environment"></param>
|
|
public static void Initialize()
|
|
{
|
|
List<DbConfigs> dbConfigs = DbConfigs.Current;
|
|
// 数据库连接配置
|
|
var iocList = new List<IocConfig>();
|
|
foreach (var item in dbConfigs)
|
|
{
|
|
iocList.Add(
|
|
new IocConfig()
|
|
{
|
|
ConfigId = item.ConfigId,
|
|
ConnectionString = item.Conn,
|
|
DbType = (IocDbType)item.DbType,
|
|
IsAutoCloseConnection = item.IsAutoCloseConnection
|
|
}
|
|
);
|
|
}
|
|
//SqlSugar.IOC 配置注入容器
|
|
SugarIocServices.AddSqlSugar(iocList);
|
|
// 配置参数
|
|
SugarIocServices.ConfigurationSugar(db =>
|
|
{
|
|
|
|
|
|
|
|
iocList.ForEach(iocConfig =>
|
|
{
|
|
//数据库Aop设置
|
|
SetSugarAop(db, iocConfig);
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 数据库Aop设置
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <param name="iocConfig"></param>
|
|
/// <param name="cache"></param>
|
|
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig)
|
|
{
|
|
var config = db.GetConnectionScope(iocConfig.ConfigId).CurrentConnectionConfig;
|
|
var showDbLog = AppSettings.Current?.ShowDbLog??true;
|
|
string configId = config.ConfigId;
|
|
////SQL执行前
|
|
db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
//if (showDbLog)
|
|
//{
|
|
// string log = $"【db{configId} SQL】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
|
|
// if (sql.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
// {
|
|
// logger.Info(log);
|
|
// }
|
|
// else if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
// {
|
|
// logger.Warn(log);
|
|
// }
|
|
// else if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("TRUNCATE", StringComparison.OrdinalIgnoreCase))
|
|
// {
|
|
// logger.Error(log);
|
|
// }
|
|
// else
|
|
// {
|
|
// log = $"【db{configId} SQL语句】dbo.{sql} {string.Join(", ", pars.Select(x => x.ParameterName + " = " + GetParsValue(x)))};\n";
|
|
// logger.Info(log);
|
|
// }
|
|
// // 计算所需时间
|
|
//}
|
|
};
|
|
//SQL执行完
|
|
db.GetConnectionScope(configId).Aop.OnLogExecuted = (sql, pars) =>
|
|
{
|
|
|
|
|
|
if (showDbLog)
|
|
{
|
|
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
|
|
string spendTime = "\n 这个sql耗时" + db.Ado.SqlExecutionTime.TotalSeconds.ToString() + "秒";
|
|
string log = $"【db{configId} SQL】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n";
|
|
if (sql.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
logger.Info(log + spendTime);
|
|
|
|
|
|
}
|
|
else if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
logger.Warn(log + spendTime);
|
|
|
|
}
|
|
else if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("TRUNCATE", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
logger.Error(log + spendTime);
|
|
|
|
}
|
|
else
|
|
{
|
|
log = $"【db{configId} SQL语句】dbo.{sql} {string.Join(", ", pars.Select(x => x.ParameterName + " = " + GetParsValue(x)))};\n";
|
|
logger.Info(log + spendTime);
|
|
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
// SQL报错
|
|
db.GetConnectionScope(configId).Aop.OnError = (ex) =>
|
|
{
|
|
//var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
|
|
|
|
string sql = "【错误SQL】" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n";
|
|
logger.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}");
|
|
};
|
|
|
|
|
|
db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) =>
|
|
{
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private static object GetParsValue(SugarParameter x)
|
|
{
|
|
if (x.DbType == System.Data.DbType.String || x.DbType == System.Data.DbType.DateTime || x.DbType == System.Data.DbType.String)
|
|
{
|
|
return "'" + x.Value + "'";
|
|
}
|
|
return x.Value;
|
|
}
|
|
|
|
}
|
|
}
|