395,986 Members | 4,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 395,986 IT Pros & Developers. It's quick & easy.

Why doesn't trace write timestamp?

Brett Romero
P: n/a
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

Apr 10 '06 #1
Share this Question
Share on Google+
5 Replies


Jon Skeet [C# MVP]
P: n/a
Brett Romero <account@cygen.com> wrote:
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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #2

Brett Romero
P: n/a
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

Apr 10 '06 #3

Jon Skeet [C# MVP]
P: n/a
Brett Romero <account@cygen.com> wrote:
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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #4

Brett Romero
P: n/a
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

Apr 10 '06 #5

Jon Skeet [C# MVP]
P: n/a
Brett Romero <account@cygen.com> wrote:
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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.