This commit is contained in:
不做码农 2022-03-17 20:21:38 +08:00
commit a47821fb69
3 changed files with 93 additions and 82 deletions

View File

@ -28,7 +28,7 @@ namespace ZR.Admin.WebApi.Extensions
{ {
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin); string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus); string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
string dbKey = Configuration[OptionsSetting.DbKey];
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]); int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]); int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
@ -45,8 +45,10 @@ namespace ZR.Admin.WebApi.Extensions
IsAutoCloseConnection = true IsAutoCloseConnection = true
} }
}); });
//每次Sql执行前事件 SugarIocServices.ConfigurationSugar(db =>
DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuting = (sql, pars) => {
#region db0
db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
{ {
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
@ -54,28 +56,33 @@ namespace ZR.Admin.WebApi.Extensions
logger.Info($"{sql}{param}"); logger.Info($"{sql}{param}");
}; };
//出错打印日志
DbScoped.SugarScope.GetConnection(0).Aop.OnError = (e) => db.GetConnection(0).Aop.OnError = (e) =>
{ {
logger.Error(e, $"执行SQL出错{e.Message}"); logger.Error(e, $"执行SQL出错{e.Message}");
}; };
//SQL执行完 //SQL执行完
DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuted = (sql, pars) => db.GetConnection(0).Aop.OnLogExecuted = (sql, pars) =>
{ {
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate) //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
}; };
#endregion
#region db1
//Db1 //Db1
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) => db.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
{ {
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
logger.Info($"Sql语句{sql}, {param}"); logger.Info($"Sql语句{sql}, {param}");
}; };
//Db1错误日志 //Db1错误日志
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) => db.GetConnection(1).Aop.OnError = (e) =>
{ {
logger.Error($"执行Sql语句失败{e.Sql},原因:{e.Message}"); logger.Error($"执行Sql语句失败{e.Sql},原因:{e.Message}");
}; };
#endregion
});
} }
/// <summary> /// <summary>

View File

@ -47,6 +47,9 @@
<target name="console" xsi:type="ColoredConsole" <target name="console" xsi:type="ColoredConsole"
layout="${date:format=MM-dd HH\:mm\:ss} | ${uppercase:${level}} | ${logger} | ${aspnet-request-iP} | ${aspnet-request-url} | ${message}"/> layout="${date:format=MM-dd HH\:mm\:ss} | ${uppercase:${level}} | ${logger} | ${aspnet-request-iP} | ${aspnet-request-url} | ${message}"/>
<target name="consoleSql" xsi:type="ColoredConsole"
layout="${date:format=MM-dd HH\:mm\:ss} | ${aspnet-request-url} ${newline} ${message}"/>
<!--写入黑洞--> <!--写入黑洞-->
<target name="blackhole" xsi:type="Null" /> <target name="blackhole" xsi:type="Null" />
</targets> </targets>
@ -59,6 +62,7 @@
<!--<logger name="System.*" writeTo="blackhole" final="true" />--> <!--<logger name="System.*" writeTo="blackhole" final="true" />-->
<!-- Quartz --> <!-- Quartz -->
<logger name="Quartz*" minlevel="Trace" maxlevel="Info" final="true" /> <logger name="Quartz*" minlevel="Trace" maxlevel="Info" final="true" />
<logger name="ZR.Admin.WebApi.Extensions.DbExtension" final="true" writeTo="consoleSql"/>
<logger name="*" minLevel="Debug" writeTo="console"/> <logger name="*" minLevel="Debug" writeTo="console"/>
<logger name="ZR.Admin.WebApi.Extensions.DbExtension" final="true" writeTo="sqlfile"/> <logger name="ZR.Admin.WebApi.Extensions.DbExtension" final="true" writeTo="sqlfile"/>

View File

@ -2,7 +2,7 @@ module.exports = {
/** /**
* 框架版本号 * 框架版本号
*/ */
version: '3.7.7', version: '3.7.8',
title: 'ZrAdmin.NET-后台管理', title: 'ZrAdmin.NET-后台管理',
/** /**
* 主题颜色 * 主题颜色