using Microsoft.IdentityModel.Logging; using SqlSugar; using System; using System.Threading.Tasks; namespace Ems.CollectService.SqlSugarCore { public class SqlGenerator { #region 参数定义 private static string ConnectionString = "Data Source=nyfw-m.db.sfcloud.local;Port=3306;Initial Catalog=sfemsrefactor;uid=highwayiot; pwd=Administrator@;min pool size=1;SslMode=none;Charset=utf8"; //private static string ConnectionString = "Data Source=43.143.98.48;Port=3306;Initial Catalog=hw_ems;uid=root; pwd=yq@123456;min pool size=1;SslMode=none;Charset=utf8"; private static SqlSugarClient sqlSugarClient = null; private static readonly object lockHelper = new object(); #endregion /// /// 连接服务器数据库 /// /// public static SqlSugarClient GetMySqlInstance() { if (sqlSugarClient == null) { lock (lockHelper) { if (sqlSugarClient == null) { sqlSugarClient = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.MySql, ConnectionString = ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, //AopEvents = new AopEvents //{ // //记录执行SQL和参数 // OnLogExecuting = (sql, p) => // { // LogHelper.SqlLog(sql); // } //} }); sqlSugarClient.Aop.OnLogExecuting = (sql, pars) => { Parallel.For(0, 1, e => { string SqlLog = ""; foreach (string item in new string[] { GetParas(pars), "【SQL语句】:" + sql }) { SqlLog = SqlLog + item; } }); }; } } } return sqlSugarClient; } private static string GetParas(SugarParameter[] pars) { string key = "【SQL参数】"; foreach (var param in pars) { key += $"{param.ParameterName}:{param.Value}\n"; } return key; } } }