123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using Base;
- using Infrastructure;
- using Infrastructure.Model;
- using SqlSugar.IOC;
- namespace SqlSugar
- {
- public static class SqlsugarSetup
- {
- private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
-
-
-
-
-
- public static void AddDb(this IServiceCollection services, IWebHostEnvironment environment)
- {
- var options = App.OptionsSetting;
- List<DbConfigs> dbConfigs = options.DbConfigs;
- 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
- });
- }
- SugarIocServices.AddSqlSugar(iocList);
- ICacheService cache = new SqlSugarCache();
- SugarIocServices.ConfigurationSugar(db =>
- {
- var u = App.User;
- if (u != null)
- {
- DataPermi.FilterData(0);
-
-
- }
- iocList.ForEach(iocConfig =>
- {
- SetSugarAop(db, iocConfig, cache);
- });
- });
- if (options.InitDb && environment.IsDevelopment())
- {
- InitTable.InitDb();
- }
- }
-
-
-
-
-
-
- private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache)
- {
- var config = db.GetConnectionScope(iocConfig.ConfigId).CurrentConnectionConfig;
- var showDbLog = AppSettings.Get<bool>("ShowDbLog");
- string configId = config.ConfigId;
- 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);
- }
- }
- };
- db.GetConnectionScope(configId).Aop.OnError = (ex) =>
- {
-
- 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) =>
- {
- };
-
- db.GetConnectionScope(configId).Aop.OnDiffLogEvent = it =>
- {
-
- var editBeforeData = it.BeforeData;
-
- var editAfterData = it.AfterData;
- var sql = it.Sql;
- var parameter = it.Parameters;
- var data = it.BusinessData;
- var time = it.Time;
- var diffType = it.DiffType;
- string name = App.UserName;
- foreach (var item in editBeforeData)
- {
- var pars = db.Utilities.SerializeObject(item.Columns.ToDictionary(it => it.ColumnName, it => it.Value));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- logger.WithProperty("title", data).Info(pars);
-
-
-
- }
- };
- db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
- {
- IsAutoRemoveDataCache = true
- };
- db.GetConnectionScope(configId).CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
- {
- DataInfoCacheService = cache,
- EntityService = (c, p) =>
- {
- if (p.IsPrimarykey == true)
- {
- p.IsNullable = false;
- }
- else if (p.ExtendedAttribute?.ToString() == "0")
- {
- p.IsNullable = false;
- }
- else
- {
- p.IsNullable = true;
- }
- }
- };
- db.GetConnectionScope(configId).Aop.OnLogExecuted = (sql, pars) =>
- {
- var sqlExecutionTime = AppSettings.Get<int>("sqlExecutionTime");
- if (db.Ado.SqlExecutionTime.TotalSeconds > sqlExecutionTime)
- {
-
- 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}";
- logger.Warn(logInfo);
- }
- };
- }
- 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;
- }
- }
- }
|