DbCommandCustomInterceptor.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Diagnostics;
  9. using Microsoft.EntityFrameworkCore.Diagnostics;
  10. using YiSha.Util;
  11. namespace YiSha.Data.EF
  12. {
  13. /// <summary>
  14. /// Sql执行拦截器
  15. /// </summary>
  16. public class DbCommandCustomInterceptor : DbCommandInterceptor
  17. {
  18. public async override Task<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
  19. {
  20. var obj = await base.NonQueryExecutingAsync(command, eventData, result, cancellationToken);
  21. return obj;
  22. }
  23. public async override Task<int> NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default)
  24. {
  25. if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000)
  26. {
  27. LogHelper.Warn("耗时的Sql:" + command.GetCommandText());
  28. }
  29. int val = await base.NonQueryExecutedAsync(command, eventData, result, cancellationToken);
  30. return val;
  31. }
  32. public async override Task<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default)
  33. {
  34. var obj = await base.ScalarExecutingAsync(command, eventData, result, cancellationToken);
  35. return obj;
  36. }
  37. public async override Task<object> ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object result, CancellationToken cancellationToken = default)
  38. {
  39. if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000)
  40. {
  41. LogHelper.Warn("耗时的Sql:" + command.GetCommandText());
  42. }
  43. var obj = await base.ScalarExecutedAsync(command, eventData, result, cancellationToken);
  44. return obj;
  45. }
  46. public async override Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
  47. {
  48. var obj = await base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
  49. return obj;
  50. }
  51. public async override Task<DbDataReader> ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default)
  52. {
  53. if (eventData.Duration.TotalMilliseconds >= GlobalContext.SystemConfig.DBSlowSqlLogTime * 1000)
  54. {
  55. LogHelper.Warn("耗时的Sql:" + command.GetCommandText());
  56. }
  57. var reader = await base.ReaderExecutedAsync(command, eventData, result, cancellationToken);
  58. return reader;
  59. }
  60. }
  61. }