Using Trace to write to a file

  • Thread starter Thread starter Simon
  • Start date Start date
S

Simon

Hi all,

I'm trying to use the Trace class to log messages to a simple text file.
I need to be able to provide a custom filename to be used. I've created
a TextWriterTraceListener and added it to the Trace.Listeners
collections but nothing ever seems to get written to the file.

The file does get created, but calling the Write methods doesn't seem to
do anything.

Can anyone tell me why the code below shouldn't work?

TextWriterTraceListener textListener = new
TextWriterTraceListener("Hello.txt");

Trace.Listeners.Add(textListener);

// Doesn't Work
Trace.WriteLine("Hello World");

// Doesn't Work
textListener.WriteLine("Hello World");

Many thanks all

Simon
 
Hello,

The writer buffers messages in memory. Call the Flush method or set the
AutoFlush property to true on the underlying writer.

Kind regards,
Henning Krause
 
In general, I hate config files, but they lend themselves nicely to tracing.

<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="TextWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener, System,
Version=2.0.50727.42, Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="C:\myFunkyTrace.log"
/>
</listeners>
</trace>
</system.diagnostics>

No need to muck around in code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top