(MVC) MVC (2018)

<<назад InterceptorLogging.vb (Contoso University).

   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 ( )
Link to this page: //www.vb-net.com/EF-missing-FAQ/Code/InterceptorLogging.vb.htm
< THANKS ME>