jimconstable jimconstable - 3 months ago 16
Vb.net Question

How does My.Application.Log.WriteEntry() relate to System.Diagnostics.Trace.WriteLine()

WriteEntry has a completely different signature to Writeline. There ia also a My.Application.Log.WriteException(). When I add the listeners programatically, WriteEntry doesn't work.

Trace.Listeners.Add(New Logging.FileLogTraceListener("stuff") With { _
.Append = False, _
.AutoFlush = True, _
.DiskSpaceExhaustedBehavior = Logging.DiskSpaceExhaustedOption.DiscardMessages, _
.Location = Logging.LogFileLocation.ExecutableDirectory, _
.TraceOutputOptions = TraceOptions.DateTime, _
.BaseFileName = GetExecutingAssembly.GetName.Name & "_" & Date.Now.ToString(ISO_LogFileNameFormat)})


Can someone explain the relationship between these functions?

Answer

To know the difference between WriteEntry and Writeline use Reflector and drill down to see the internal implementation. The System.Diagnostic.Trace.WriteLine:

Public Shared Sub WriteLine(ByVal value As Object)
    If TraceInternal.UseGlobalLock Then
        SyncLock TraceInternal.critSec
            Dim listener As TraceListener
            For Each listener In TraceInternal.Listeners
                listener.WriteLine(value)
                If TraceInternal.AutoFlush Then
                    listener.Flush
                End If
            Next
            Return
        End SyncLock
    End If
    Dim listener2 As TraceListener
    For Each listener2 In TraceInternal.Listeners
        If Not listener2.IsThreadSafe Then
            SyncLock listener2
                listener2.WriteLine(value)
                If TraceInternal.AutoFlush Then
                    listener2.Flush
                End If
                Continue For
            End SyncLock
        End If
        listener2.WriteLine(value)
        If TraceInternal.AutoFlush Then
            listener2.Flush
        End If
    Next
End Sub

The My.Application.Log.WriteEntry is just a LOP syntax but its really pointing to the namespace Microsoft.VisualBasic.Logging.Log.WriteEntry:

Public Sub WriteEntry(ByVal message As String, ByVal severity As TraceEventType, ByVal id As Integer)
    If (message Is Nothing) Then
        message = ""
    End If
    Me.m_TraceSource.TraceEvent(severity, id, message)
End Sub

To set it up programmatically you can do this:

Dim listener As New FileLogTraceListener()
listener.Location = LogFileLocation.TempDirectory
listener.BaseFileName = "ConsoleApp_"
listener.Append = True
listener.Delimiter = "|"
listener.AutoFlush = True

In general though you'll configure it in the config file:

<system.diagnostics>
   <sources>
      <source name="TestSrc">
         <listeners>
               <add name="FileLogger" type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
                           Microsoft.VisualBasic,Version=8.0.0.0,Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </listeners>
     </source>
   </sources>
</system.diagnostics>

Then you can use it like this:

Debug.Listeners.Add(listener)
Debug.WriteLine("Testing 1 2 3")
Comments