EasyCommandInterceptor.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Data;
  2. using System.Data.Common;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using EasyDevCore.Common;
  7. using Microsoft.EntityFrameworkCore.Diagnostics;
  8. namespace EasyDevCore.Database
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. /// <seealso cref="Microsoft.EntityFrameworkCore.Diagnostics.DbCommandInterceptor" />
  14. public class EasyCommandInterceptor: DbCommandInterceptor
  15. {
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="EasyCommandInterceptor"/> class.
  18. /// </summary>
  19. public EasyCommandInterceptor()
  20. {
  21. }
  22. /// <summary>
  23. /// Called when execution of a command has failed with an exception.
  24. /// </summary>
  25. /// <param name="command">The command.</param>
  26. /// <param name="eventData">Contextual information about the command and execution.</param>
  27. /// <param name="cancellationToken">The cancellation token.</param>
  28. /// <returns>
  29. /// A <see cref="T:System.Threading.Tasks.Task" /> representing the asynchronous operation.
  30. /// </returns>
  31. public override Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default)
  32. {
  33. if (eventData.Exception != null) {
  34. eventData.Exception.Data["CommandText"] = GetSQLText(command);
  35. }
  36. return base.CommandFailedAsync(command, eventData, cancellationToken);
  37. }
  38. /// <summary>
  39. /// Called when execution of a command has failed with an exception.
  40. /// </summary>
  41. /// <param name="command">The command.</param>
  42. /// <param name="eventData">Contextual information about the command and execution.</param>
  43. public override void CommandFailed(DbCommand command, CommandErrorEventData eventData)
  44. {
  45. if(eventData.Exception != null) {
  46. eventData.Exception.Data["CommandText"] = GetSQLText(command);
  47. }
  48. base.CommandFailed(command, eventData);
  49. }
  50. /// <summary>
  51. /// Gets the SQL text.
  52. /// </summary>
  53. /// <param name="command">The command.</param>
  54. /// <returns></returns>
  55. private static string GetSQLText(DbCommand command)
  56. {
  57. StringBuilder sb = new();
  58. sb.AppendLine($"Source : {command.Connection.DataSource}");
  59. sb.AppendLine($"Database : {command.Connection.Database}");
  60. sb.AppendLine($"Connection time out : {command.Connection.ConnectionTimeout}");
  61. sb.AppendLine($"Query Type : {command.CommandType}");
  62. sb.AppendLine($"Command time out : {command.CommandTimeout}");
  63. sb.AppendLine($"SQL Text : {command.CommandText}");
  64. foreach(DbParameter p in command.Parameters) {
  65. sb.AppendLine(string.Format(" ?{0}{1}({2}) = {3}", (p.Direction == ParameterDirection.Input ? "" : "="), p.ParameterName, p.DbType, (p.DbType.ToString().HasAny(System.StringComparison.InvariantCultureIgnoreCase, "string", "date", "time") ? $"'{p.Value}'" : $"{p.Value}")));
  66. }
  67. return sb.ToString();
  68. }
  69. }
  70. }