(MVC) MVC (2017)

InterceptorLogging (повернутися на головну сторінку проєкту)

InterceptorLogging.vb - EF Command Interceptor з таймером. Має три метода ScalarExecuted (запит формується з EF методами Linq First/FirstOrDefault/Single/SingleOrDefault та інш.), ReaderExecuted (запит формується з EF методами ForEach/ToLiat/Sum/Count та інші.), NonQueryExecuting (можливо запит теж формується у якихось випадках).



   1:  Imports System.Data.Common
   2:  Imports System.Data.Entity.Infrastructure.Interception
   3:  Imports System.Diagnostics
   4:   
   5:  Namespace Log1
   6:   
   7:      Public Class InterceptorLogging
   8:          Inherits DbCommandInterceptor
   9:   
  10:          Private _logger As ILogger = New Logger1()
  11:   
  12:          Private ReadOnly _stopwatch As Stopwatch = New Stopwatch()
  13:   
  14:          Public Overrides Sub ScalarExecuting(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of Object))
  15:              MyBase.ScalarExecuting(command, interceptionContext)
  16:              _stopwatch.Restart()
  17:          End Sub
  18:   
  19:          Public Overrides Sub ScalarExecuted(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of Object))
  20:              _stopwatch.[Stop]()
  21:              If interceptionContext.Exception IsNot Nothing Then
  22:                  _logger.[Error](interceptionContext.Exception, "Error executing command: {0}", command.CommandText)
  23:              Else
  24:                  _logger.TraceApi("SQL Database", "Interceptor.ScalarExecuted", _stopwatch.Elapsed, "Command: {0}: ", command.CommandText)
  25:              End If
  26:   
  27:              MyBase.ScalarExecuted(command, interceptionContext)
  28:          End Sub
  29:   
  30:          Public Overrides Sub NonQueryExecuting(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of Integer))
  31:              MyBase.NonQueryExecuting(command, interceptionContext)
  32:              _stopwatch.Restart()
  33:          End Sub
  34:   
  35:          Public Overrides Sub NonQueryExecuted(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of Integer))
  36:              _stopwatch.[Stop]()
  37:              If interceptionContext.Exception IsNot Nothing Then
  38:                  _logger.[Error](interceptionContext.Exception, "Error executing command: {0}", command.CommandText)
  39:              Else
  40:                  _logger.TraceApi("SQL Database", "Interceptor.NonQueryExecuted", _stopwatch.Elapsed, "Command: {0}: ", command.CommandText)
  41:              End If
  42:   
  43:              MyBase.NonQueryExecuted(command, interceptionContext)
  44:          End Sub
  45:   
  46:          Public Overrides Sub ReaderExecuting(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of DbDataReader))
  47:              MyBase.ReaderExecuting(command, interceptionContext)
  48:              _stopwatch.Restart()
  49:          End Sub
  50:   
  51:          Public Overrides Sub ReaderExecuted(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of DbDataReader))
  52:              _stopwatch.[Stop]()
  53:              If interceptionContext.Exception IsNot Nothing Then
  54:                  _logger.[Error](interceptionContext.Exception, "Error executing command: {0}", command.CommandText)
  55:              Else
  56:                  _logger.TraceApi("SQL Database", "Interceptor.ReaderExecuted", _stopwatch.Elapsed, "Command: {0}: ", command.CommandText)
  57:              End If
  58:   
  59:              MyBase.ReaderExecuted(command, interceptionContext)
  60:          End Sub
  61:      End Class
  62:   
  63:  End Namespace


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21
Link to this page: //www.vb-net.com/EF-missing-FAQ/InterceptorLogging.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>