Why doesn't trace write timestamp?

B

Brett Romero

I have this in an app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener" traceOutputOptions="Timestamp"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\TestConsole\MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>

The code I'm using to write is this:

Debug.WriteLine("Hello config file.");

I just want to output variable values but also would like a time stamp
appended with every .Write or .WriteLine. First, what is the above
timestamp option doing (this came from intellisense) and how do I get
the behavior I'm looking for?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
I have this in an app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener" traceOutputOptions="Timestamp"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\TestConsole\MyListener.log"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>

The code I'm using to write is this:

Debug.WriteLine("Hello config file.");

I just want to output variable values but also would like a time stamp
appended with every .Write or .WriteLine. First, what is the above
timestamp option doing (this came from intellisense) and how do I get
the behavior I'm looking for?

Having had a little experiment, it looks like this is only used when
you call Trace.TraceError, Trace.TraceInformation or Trace.TraceWarning
- not the Write* methods.
 
B

Brett Romero

I see: I'm using:

Trace.Flush();
Trace.TraceInformation("");

and see this entry in the log file:
TestConsole.vshost.exe Information: 0 :
Timestamp=1620913894489365

How exactly can the readable date/time be written into the log?

Also, flush() doesn't seem to be doing anything. I still have all of
the previous log entries there. How can I clean out the log file on
each debug session or is this something I have to do completely
manually (streamwriter)?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
I see: I'm using:

Trace.Flush();
Trace.TraceInformation("");

and see this entry in the log file:
TestConsole.vshost.exe Information: 0 :
Timestamp=1620913894489365

How exactly can the readable date/time be written into the log?

Have you tried DateTime as an option instead of Timestamp?
Also, flush() doesn't seem to be doing anything. I still have all of
the previous log entries there. How can I clean out the log file on
each debug session or is this something I have to do completely
manually (streamwriter)?

Flush doesn't do what you think it does - it makes sure that any cached
data is flushed to disk.
 
B

Brett Romero

I tried datetime but that didn't do anything. Nothing is outputting in
that regard:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>

<trace autoflush="true" indentsize="2">
<listeners>
<add name="myListener" traceOutputOptions="DateTime"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Test\bin\Debug\MyListener.log"/>
</listeners>
</trace>

</system.diagnostics>
</configuration>

Any other suggestions on the timestamp?

I'd also like to output only certain code. It seems to be outputting
all errors. In other words, how do I configure a Trace in the
app.config file then reference it in code the same way I do Debug? For
example, the trace may be named myTrace and I can do
myTrace.WriteLine(). The log file will contain only entries that I
explicitly reference via myTrace.

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
I tried datetime but that didn't do anything. Nothing is outputting in
that regard:

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's some sample code which uses the same configuration as yours:

using System;
using System.Diagnostics;

class Test
{
static void Main()
{
Trace.TraceInformation("Hello");
Trace.Flush();
}

}

Here's the result:
test Information: 0 : Hello
DateTime=2006-04-10T18:23:25.3437500Z

(Of course, you need to compile with TRACE defined.)
I'd also like to output only certain code. It seems to be outputting
all errors. In other words, how do I configure a Trace in the
app.config file then reference it in code the same way I do Debug? For
example, the trace may be named myTrace and I can do
myTrace.WriteLine(). The log file will contain only entries that I
explicitly reference via myTrace.

You can use Trace.Listeners["myTrace"] to get a specific TraceListener,
but I don't think you would normally do that. (TraceListener doesn't
seem to encourage you to use it directly.)

Perhaps you should look at other logging frameworks, such as Log4Net?
 

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

Top