Objectmix
Tags Register Mark Forums Read

Using Trace to write to a file : DOTNET

This is a discussion on Using Trace to write to a file within the DOTNET forums in Framework and Interface Programming category; 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...


Object Mix > Framework and Interface Programming > DOTNET > Using Trace to write to a file

DOTNET Discussion forums related to Microsoft Dot net technologies, CSharp and other related items

Reply

 

LinkBack Thread Tools
  #1  
Old 07-15-2007, 12:51 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Using Trace to write to a file

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
  #2  
Old 07-15-2007, 02:07 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Using Trace to write to a file

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

"Simon" <nothanks@hotmail.com> wrote in message
news:e4c3qiwxHHA.4928@TK2MSFTNGP03.phx.gbl...
> 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


  #3  
Old 07-15-2007, 03:18 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default RE: Using Trace to write to a file


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.

  #4  
Old 07-15-2007, 11:59 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Using Trace to write to a file

You need to Call the Flush Method
"Simon" <nothanks@hotmail.com> wrote in message
news:e4c3qiwxHHA.4928@TK2MSFTNGP03.phx.gbl...
> 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
>


Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
NUnit GUI Runner - Trace Output and Internal Trace?? usenet CSharp 0 11-19-2007 04:39 PM
Large MOSS trace log file usenet Sharepoint 3 10-18-2007 09:17 AM
Trace.Write..... usenet DOTNET 3 10-05-2006 04:36 PM
Re: Trace: Can anyone suggest a good tool to catch trace messages? usenet DOTNET 0 10-27-2003 04:04 PM
Re: Trace: Can anyone suggest a good tool to catch trace messages? usenet DOTNET 0 10-12-2003 07:46 PM


All times are GMT -5. The time now is 08:48 AM.

Managed by Infnx Pvt Ltd.