473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jul 15 '07 #1
3 2876
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" <no******@hotmail.comwrote in message
news:e4**************@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
Jul 15 '07 #2

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.

Jul 15 '07 #3
You need to Call the Flush Method
"Simon" <no******@hotmail.comwrote in message
news:e4**************@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
Jul 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: svenn.are | last post by:
Hi, has anybody thought of / already used graphviz to convert the output of trace.py into a graph? I looked at PyUMLGraph, but 1. PyUMLGraph does not successfully create a dot file, and 2. I...
3
by: Jens Alenius | last post by:
I've been looking at the Trace and TraceSwitch Classes i C# and I thrying to make it act more like java log4j. What I want is to add two different tracelisteners (like EventLogTraceListener and...
1
by: Jiho Han | last post by:
I am wondering whether asp.net applications can write trace information using the System.Diagnostics.Trace class, and also whether in web.config, the <system.diagnostics> element and its sub...
1
by: Martin | last post by:
Hi, is it a wise thing to do to place trace listeners (that write to a text file) into an asp.net application. I wish to do this for testing so that a user may vist as many different pages as...
3
by: Bruno Paquette | last post by:
I have installed visual studio .net and now i started to write the first few examples from the 70-315 book by Kalani. It looks like all server side code that i put between <% %> doesnt process...
3
by: Schorschi | last post by:
Ok, can someone point out to me why the following code does not work? In compiles, no errors, but MemoryStream never seems to receive any data? Dim theMemory As MemoryStream, _ theListener As...
6
by: Zytan | last post by:
According to http://www.15seconds.com/issue/020910.htm I am doing this in the c'tor of a 'logfile' class: objStream = new System.IO.FileStream(logFilename, System.IO.FileMode.OpenOrCreate);...
0
by: Paul | last post by:
This is not a question, More a heads up in case anyone else gets this problem. We have an HTTP Handler that spits out PDF files. During development we chose to use the .ashx file to provide...
0
by: =?Utf-8?B?YWtz?= | last post by:
I've configured Trace switch in my app.config file: DebugLevel = new TraceSwitch("DebugLevel", "Trace Level for Entire Application"); and have created and initialized a trace listener in my...
3
by: Simon | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.