22using System ;
33using System . Diagnostics ;
44using System . Linq ;
5+ using Infrastructure . Logging ;
56using Infrastructure . Web ;
67
78namespace DbModel
@@ -13,8 +14,10 @@ public static MysqlDbContext<AntEntity> DB
1314 get
1415 {
1516 var db = new MysqlDbContext < AntEntity > ( "ant_mysql" ) ;
17+ #if DEBUG
1618 db . IsEnableLogTrace = true ;
1719 db . OnLogTrace = OnCustomerTraceConnection ;
20+ #endif
1821 return db ;
1922 }
2023 }
@@ -23,24 +26,38 @@ public static MysqlDbContext<AntEntity> DB
2326 /// 记录sql
2427 /// </summary>
2528 /// <param name="customerTraceInfo"></param>
26- public static void OnCustomerTraceConnection ( CustomerTraceInfo customerTraceInfo )
29+ private static void OnCustomerTraceConnection ( CustomerTraceInfo customerTraceInfo )
2730 {
28- string sql = customerTraceInfo . SqlText ;
2931 try
3032 {
31- sql = customerTraceInfo . CustomerParams . Aggregate ( customerTraceInfo . SqlText ,
32- ( current , item ) => current . Replace ( item . Key , item . Value . Value . ToString ( ) ) ) ;
33+ string sql = Environment . NewLine + "#####################################################" + Environment . NewLine + customerTraceInfo . SqlText ;
34+ try
35+ {
36+ sql = customerTraceInfo . CustomerParams . Aggregate ( customerTraceInfo . SqlText ,
37+ ( current , item ) => current . Replace ( item . Key , item . Value . Value . ToString ( ) ) ) ;
38+ }
39+ catch ( Exception )
40+ {
41+ //ignore
42+ }
43+ sql += Environment . NewLine ;
44+ foreach ( var detail in customerTraceInfo . RunTimeList )
45+ {
46+
47+ var sencond = ( int ) detail . Duration . TotalSeconds ;
48+ var time = sencond + "秒" ;
49+ if ( sencond < 1 )
50+ {
51+ time = detail . Duration . TotalMilliseconds + "豪秒" ;
52+ }
53+ sql += $ "Server:{ detail . Server } ,DB名称:{ detail . DbName } , 执行时间:{ time } " + Environment . NewLine + "#####################################################" + Environment . NewLine ;
54+ LogHelper . Info ( "SQL" , sql ) ;
55+ }
3356 }
3457 catch ( Exception )
3558 {
3659 //ignore
3760 }
38- sql += Environment . NewLine ;
39- foreach ( var detail in customerTraceInfo . RunTimeList )
40- {
41- sql += $ "Server:{ detail . Server } ,DB名称:{ detail . DbName } , 执行时间:{ detail . Duration . TotalSeconds } 秒" + Environment . NewLine ;
42- Debug . Write ( sql ) ;
43- }
4461 }
4562 }
4663}
0 commit comments