开启默认sugar超时时间

This commit is contained in:
2023-05-24 11:52:16 +08:00
parent 65072abc04
commit 1b65a7a9e5

View File

@@ -13,60 +13,60 @@ namespace Microsoft.Extensions.DependencyInjection;
/// </summary> /// </summary>
public static class ConfigureSqlSugarExtensions public static class ConfigureSqlSugarExtensions
{ {
public static IServiceCollection ConfigureSqlSugar(this IServiceCollection services) public static IServiceCollection ConfigureSqlSugar(this IServiceCollection services)
{
// 获取选项
ConnectionStringsOptions conn = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
List<ConnectionConfig> connectConfigList = new List<ConnectionConfig>();
var DBType = (DbType)Enum.Parse(typeof(DbType), conn.DBType);
// 默认数据库
connectConfigList.Add(new ConnectionConfig
{ {
ConnectionString = string.Format(conn.DefaultConnection, conn.Host, conn.Port, conn.DBName, conn.UserName, conn.Password), // 获取选项
DbType = DBType, ConnectionStringsOptions conn = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
IsAutoCloseConnection = true,
ConfigId = conn.ConfigId,
InitKeyType = InitKeyType.Attribute,
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true // 自动清理缓存
},
});
services.AddSqlSugar(connectConfigList, client => List<ConnectionConfig> connectConfigList = new List<ConnectionConfig>();
{ var DBType = (DbType)Enum.Parse(typeof(DbType), conn.DBType);
//connectConfigList.ForEach(config => // 默认数据库
//{ connectConfigList.Add(new ConnectionConfig
// var db = ((SqlSugarScope)client).GetConnectionScope((string)config.ConfigId); {
ConnectionString = string.Format(conn.DefaultConnection, conn.Host, conn.Port, conn.DBName, conn.UserName, conn.Password),
DbType = DBType,
IsAutoCloseConnection = true,
ConfigId = conn.ConfigId,
InitKeyType = InitKeyType.Attribute,
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true // 自动清理缓存
},
});
// // 设置超时时间 services.AddSqlSugar(connectConfigList, client =>
// db.Ado.CommandTimeOut = 30; {
// db.Aop.OnLogExecuted = (sql, pars) => connectConfigList.ForEach(config =>
// { {
// var oldColor = Console.ForegroundColor; var db = ((SqlSugarScope)client).GetConnectionScope((string)config.ConfigId);
// Console.ForegroundColor = ConsoleColor.Green;
// var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars); // 设置超时时间
// Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms"); db.Ado.CommandTimeOut = 30;
// Console.WriteLine(finalSql); //db.Aop.OnLogExecuted = (sql, pars) =>
// Console.ForegroundColor = oldColor; //{
// if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000) // var oldColor = Console.ForegroundColor;
// { // Console.ForegroundColor = ConsoleColor.Green;
// Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql); // var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
// } // Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
// Console.WriteLine(); // Console.WriteLine(finalSql);
// }; // Console.ForegroundColor = oldColor;
// db.Aop.OnError = (ex) => // if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
// { // {
// Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres)); // Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
// }; // }
//}); // Console.WriteLine();
}); //};
services.AddConfigurableOptions<ConnectionStringsOptions>(); //db.Aop.OnError = (ex) =>
services.AddConfigurableOptions<TenantOptions>(); //{
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
//};
});
});
services.AddConfigurableOptions<ConnectionStringsOptions>();
services.AddConfigurableOptions<TenantOptions>();
services.AddUnitOfWork<SqlSugarUnitOfWork>();
return services; return services;
} }
} }