diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 808841c0b..82b8d9b54 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -51,9 +51,10 @@ namespace SqlSugar public virtual CommandType CommandType { get; set; } public virtual bool IsEnableLogEvent { get; set; } public virtual bool IsClearParameters { get; set; } - public virtual Action LogEventStarting { get; set; } - public virtual Action LogEventCompleted { get; set; } + public virtual Action LogEventStarting { get; set; } + public virtual Action LogEventCompleted { get; set; } public virtual Func> ProcessingEventStartingSQL { get; set; } + public virtual Action ErrorEvent { get; set; } #endregion #region Connection @@ -241,6 +242,8 @@ namespace SqlSugar } catch (Exception ex) { + if (ErrorEvent != null) + ErrorEvent(ex); throw ex; } finally @@ -250,18 +253,27 @@ namespace SqlSugar } public virtual IDataReader GetDataReader(string sql, params SugarParameter[] parameters) { - var isSp = this.CommandType == CommandType.StoredProcedure; - if (this.ProcessingEventStartingSQL != null) - ExecuteProcessingSQL(ref sql, parameters); - ExecuteBefore(sql, parameters); - IDbCommand sqlCommand = GetCommand(sql, parameters); - IDataReader sqlDataReader = sqlCommand.ExecuteReader(this.IsClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default); - if (isSp) - DataReaderParameters = sqlCommand.Parameters; - if (this.IsClearParameters) - sqlCommand.Parameters.Clear(); - ExecuteAfter(sql, parameters); - return sqlDataReader; + try + { + var isSp = this.CommandType == CommandType.StoredProcedure; + if (this.ProcessingEventStartingSQL != null) + ExecuteProcessingSQL(ref sql, parameters); + ExecuteBefore(sql, parameters); + IDbCommand sqlCommand = GetCommand(sql, parameters); + IDataReader sqlDataReader = sqlCommand.ExecuteReader(this.IsClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default); + if (isSp) + DataReaderParameters = sqlCommand.Parameters; + if (this.IsClearParameters) + sqlCommand.Parameters.Clear(); + ExecuteAfter(sql, parameters); + return sqlDataReader; + } + catch (Exception ex) + { + if (ErrorEvent != null) + ErrorEvent(ex); + throw ex; + } } public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] parameters) { @@ -282,6 +294,8 @@ namespace SqlSugar } catch (Exception ex) { + if (ErrorEvent != null) + ErrorEvent(ex); throw ex; } finally @@ -306,6 +320,8 @@ namespace SqlSugar } catch (Exception ex) { + if (ErrorEvent != null) + ErrorEvent(ex); throw ex; } finally @@ -578,7 +594,7 @@ namespace SqlSugar { if (this.IsEnableLogEvent) { - Action action = LogEventStarting; + Action action = LogEventStarting; if (action != null) { if (parameters == null || parameters.Length == 0) @@ -587,7 +603,7 @@ namespace SqlSugar } else { - action(sql, this.Context.Utilities.SerializeObject(parameters.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() }))); + action(sql, parameters); } } } @@ -606,7 +622,7 @@ namespace SqlSugar } if (this.IsEnableLogEvent) { - Action action = LogEventCompleted; + Action action = LogEventCompleted; if (action != null) { if (parameters == null || parameters.Length == 0) @@ -615,7 +631,7 @@ namespace SqlSugar } else { - action(sql, this.Context.Utilities.SerializeObject(parameters.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() }))); + action(sql, parameters); } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs new file mode 100644 index 000000000..95020c962 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SqlSugar +{ + public class AopProvider + { + private AopProvider() { } + public AopProvider(SqlSugarClient context) + { + this.Context = context; + this.Context.Ado.IsEnableLogEvent = true; + } + private SqlSugarClient Context { get; set; } + public Action OnError { set { this.Context.Ado.ErrorEvent = value; } } + public Action OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } } + public Action OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } } + public Func> OnExecutingChangeSql { set { this.Context.Ado.ProcessingEventStartingSQL = value; } } + } +} diff --git a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs index d5c22b3fe..df018f822 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs @@ -23,9 +23,10 @@ namespace SqlSugar IDataParameterCollection DataReaderParameters { get; set; } CommandType CommandType { get; set; } bool IsEnableLogEvent { get; set; } - Action LogEventStarting { get; set; } - Action LogEventCompleted { get; set; } + Action LogEventStarting { get; set; } + Action LogEventCompleted { get; set; } Func> ProcessingEventStartingSQL { get; set; } + Action ErrorEvent { get; set; } bool IsClearParameters { get; set; } int CommandTimeOut { get; set; } IDbBind DbBind { get; } diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index dec6ce9e1..1ee9dd6fd 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -56,6 +56,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index d039201cd..f1a65154d 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -62,6 +62,10 @@ namespace SqlSugar } #endregion + #region Aop Log Methods + public virtual AopProvider Aop { get { return new AopProvider(this.Context); } } + #endregion + #region Util Methods [Obsolete("Use SqlSugarClient.Utilities")] public virtual IRewritableMethods RewritableMethods