Trace Listener Issues - Am I missing something

  • Thread starter Thread starter prowler
  • Start date Start date
P

prowler

Hello,

I'm trying to do something straightforward - trying to
implement trace listeners to pepper my code with traces
triggered by trace switches. I get two problems:

1. The trace.write statements never get written to the
stream (file), it stays 0 bytes. Interestingly,
debug.write statements work.

2. If I use Trace.WriteLine I get a compile error
(intellsense too does not show the method!).

I tried to #define a trace constant at the top of my file,
no effect. Here's my code, any help appreciated...

config file:

<configuration>

<system.diagnostics>
<switches>
<add name="ATraceSwitch"
value="4" />
</switches>
</system.diagnostics>

<system.web>


public class WebForm1 : System.Web.UI.Page
{
static TraceSwitch traceSwitch = new
TraceSwitch("ATraceSwitch", "Demo trace switch");

protected System.Web.UI.WebControls.Label
Label1;

private void Page_Load(object sender,
System.EventArgs e)
{
System.IO.FileStream myTraceLog =
new
System.IO.FileStream
(@"C:\myTraceLog.txt",

System.IO.FileMode.OpenOrCreate);
// Creates the new trace listener.

System.Diagnostics.TextWriterTraceListener
myListener =
new
System.Diagnostics.TextWriterTraceListener(myTraceLog);


System.Diagnostics.Trace.Listeners.Add(myListener);

// traceSwitch.Level =
TraceLevel.Info;

if (traceSwitch.TraceVerbose)
{
Response.Write("verbose");
//Debug.WriteLine("debug
verbose message");
Trace.Write
("traceSwitch.TraceInfo is verbose nabled");
}

if (traceSwitch.TraceInfo)
{
Response.Write("info");
Trace.Write
("traceSwitch.TraceError is info enabled");
}

myListener.Flush();
myTraceLog.Close();
}
 
Thanks for your reply...unfortunately, autoflush did not
do it! Stumped and still trying to fix :-)
 
Instead of calling:

myListener.Flush();

try using:

Trace.Flush();

That will flush any buffered trace output to all the listeners.

Gregg Walker
 
It had to be something big picture! I was not
distinguishing between Page.Trace and
System.Diagnostics.Trace! I should've been writing to
Diagnostics.Trace. My messages were going into the ugly
Page Trace you never want your users to see. Thanks you
very much for your responses.
 
Back
Top