Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using a text listener to log messages

0.00/5 (No votes)
10 Nov 2011 1  
How to use the TextWriterTraceListener in Vulcan.net

Here is a very simple way to log information from your program. It works by setting up a "listener" that will intercept any trace.write command and send them to the filestream that you open. I use this method to write diagnostic messages.


C++
//
// Start.prg
//
#USING System.io
#USING System.Diagnostics
FUNCTION Start() AS VOID
    LOCAL objTraceListener AS TextWriterTraceListener
    LOCAL XferLog          AS FileStream
    LOCAL cFile            AS STRING

    cFile := "c:\test.log"

    // create the log file
    XferLog := 
System.IO.FileStream{System.IO.Path.Combine(System.Environment.CurrentDirectory, 
cFile), System.IO.FileMode.Create}
    objTraceListener := System.Diagnostics.TextWriterTraceListener{XferLog}
    System.Diagnostics.Trace.Listeners:Add(objTraceListener)

    Trace.WriteLine("Starting Log")
    Trace.WriteLine("This is a test")
    Trace.WriteLine("Leaving Log File")

    // close our log file now that we are done
    IF (XferLog != NULL)
       System.Diagnostics.Trace.Flush()
       XferLog:Close()
       System.Diagnostics.Trace.Listeners:Remove(objTraceListener)
    ENDIF

    RETURN

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here