Click here to Skip to main content

Using a text listener to log messages

How to use the TextWriterTraceListener in Vulcan.net
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

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.

//
// 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
Posted 9 Nov '11
Edited 10 Nov '11

Comments
GeorgeNT - 11 Nov '11
Reason for my vote of 5 Good idea. Also, one of the first samples in Vulcan.NET.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. SAKryukov (503)
  2. Varun Sareen (418)
  3. CRDave1988 (410)
  1. SAKryukov (13,971)
  2. OriginalGriff (8,238)
  3. Christian Graus (7,396)
  4. thatraja (5,300)
  5. Abhinav S (5,103)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --

Advertise | Privacy | Mobile
Web03 | 2.5.120222.1 | Last Updated 10 Nov 2011
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid