jimconstable jimconstable - 4 months ago 21
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?


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
                If TraceInternal.AutoFlush Then
                End If
        End SyncLock
    End If
    Dim listener2 As TraceListener
    For Each listener2 In TraceInternal.Listeners
        If Not listener2.IsThreadSafe Then
            SyncLock listener2
                If TraceInternal.AutoFlush Then
                End If
                Continue For
            End SyncLock
        End If
        If TraceInternal.AutoFlush Then
        End If
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:

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

Then you can use it like this:

Debug.WriteLine("Testing 1 2 3")