(MVC) MVC (2017)

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

Logger1.vb - логер, який пише у вікно Immediate window Visual Studio та у стандартний .NET лог. Неважко зробити й інший логер, який буде зберігати протокол запитів до SQL-серверу наприклад у текстовий файл.



   1:  Imports System.Diagnostics
   2:   
   3:  Namespace Log1
   4:   
   5:      Public Class Logger1
   6:          Implements ILogger
   7:   
   8:          Public Sub Information(message As String) Implements ILogger.Information
   9:              Debug.WriteLine(message)
  10:              Trace.TraceInformation(message)
  11:          End Sub
  12:   
  13:          Public Sub Information(fmt As String, ParamArray vars() As Object) Implements ILogger.Information
  14:              Debug.WriteLine(fmt, vars)
  15:              Trace.TraceInformation(fmt, vars)
  16:          End Sub
  17:   
  18:          Public Sub Information(exception As Exception, fmt As String, ParamArray vars() As Object) Implements ILogger.Information
  19:              Debug.WriteLine(FormatExceptionMessage(exception, fmt, vars))
  20:              Trace.TraceInformation(FormatExceptionMessage(exception, fmt, vars))
  21:          End Sub
  22:   
  23:          Public Sub Warning(message As String) Implements ILogger.Warning
  24:              Debug.WriteLine(message)
  25:              Trace.TraceWarning(message)
  26:          End Sub
  27:   
  28:          Public Sub Warning(fmt As String, ParamArray vars() As Object) Implements ILogger.Warning
  29:              Debug.WriteLine(fmt, vars)
  30:              Trace.TraceWarning(fmt, vars)
  31:          End Sub
  32:   
  33:          Public Sub Warning(exception As Exception, fmt As String, ParamArray vars() As Object) Implements ILogger.Warning
  34:              Debug.WriteLine(FormatExceptionMessage(exception, fmt, vars))
  35:              Trace.TraceWarning(FormatExceptionMessage(exception, fmt, vars))
  36:          End Sub
  37:   
  38:          Public Sub [Error](message As String) Implements ILogger.Error
  39:              Debug.WriteLine(message)
  40:              Trace.TraceError(message)
  41:          End Sub
  42:   
  43:          Public Sub [Error](fmt As String, ParamArray vars() As Object) Implements ILogger.Error
  44:              Debug.WriteLine(fmt, vars)
  45:              Trace.TraceError(fmt, vars)
  46:          End Sub
  47:   
  48:          Public Sub [Error](exception As Exception, fmt As String, ParamArray vars() As Object) Implements ILogger.Error
  49:              Debug.WriteLine(FormatExceptionMessage(exception, fmt, vars))
  50:              Trace.TraceError(FormatExceptionMessage(exception, fmt, vars))
  51:          End Sub
  52:   
  53:          Public Sub TraceApi(componentName As String, method As String, timespan As TimeSpan) Implements ILogger.TraceApi
  54:              TraceApi(componentName, method, timespan, "")
  55:          End Sub
  56:   
  57:          Public Sub TraceApi(componentName As String, method As String, timespan As TimeSpan, fmt As String, ParamArray vars() As Object) Implements ILogger.TraceApi
  58:              TraceApi(componentName, method, timespan, String.Format(fmt, vars))
  59:          End Sub
  60:   
  61:          Public Sub TraceApi(componentName As String, method As String, timespan As TimeSpan, properties As String) Implements ILogger.TraceApi
  62:              Dim message As String = String.Concat("Component:", componentName, ";Method:", method, ";Timespan:", timespan.ToString(), ";Properties:", properties)
  63:              Debug.WriteLine(message)
  64:              Trace.TraceInformation(message)
  65:          End Sub
  66:   
  67:          Private Shared Function FormatExceptionMessage(ByVal exception As Exception, ByVal fmt As String, ByVal vars As Object()) As String
  68:              Dim sb = New StringBuilder()
  69:              sb.Append(String.Format(fmt, vars))
  70:              sb.Append(" Exception: ")
  71:              sb.Append(exception.ToString())
  72:              Return sb.ToString()
  73:          End Function
  74:      End Class
  75:  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/Logger1.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>