2

How can I format the Timestamp data that is printed when you set the TraceOutputOptions = TraceOptions.Timestamp?

Im getting something like: Timestamp=41329240725 (real value that was written on the output text file)

EDITED: I want something like 10:22:34.32938. How shoud I configure the TextWriterTraceListener to achieve that?

Pedro77
  • 5,176
  • 7
  • 61
  • 91
  • But how would you want to format it? `Timestamp` is indeed a `long` value, and its actual meaning depends on the time source (high performance counter or system timer) and on the frequency of the high performance counter if one is used. – Frédéric Hamidi Oct 07 '11 at 16:05

2 Answers2

2

Do you really want to log the time that the message was written? If so, you want to use TraceOptions.DateTime. Note, that according to MSDN, the time is written as UTC.

If you want more control over the format of the time (including if you want it to be expressed in something other than UTC), then you will probably have write your own custom TraceListener, or find one that will do what you want.

One useful add on for System.Diagnostics is Ukadc.Diagnostics. With it you can easily add custom formatting to your log messages (similar to what you can do with log4net and NLog).

Here are some other links to answers that I have provided in the past to logging questions that you might find useful:

When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

When do I need more than one TraceSource in code?

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • Yes I want. Now Im getting this: DateTime=2011-10-07T17:38:33.9825856Z. It is ok. Do your prefer Ukadc.Diagnostics than log4net ? Im tryng to trace first without any other 3rd part library. – Pedro77 Oct 07 '11 at 17:42
  • 1
    Right now my personal preference, if using a 3rd party library is not a problem, is NLog. A new update has been released recently. NLog and log4net are very close in functionality. You probably won't go wrong using either one. Another option is to use the Common.Logging (http://netcommon.sourceforge.net/) logging abstraction. That way you are not tied to a specific logging library. Note that Common.Logging has not released a new version yet that uses NLog's most recent version. With Common.Logging you can ... – wageoghe Oct 07 '11 at 17:48
  • 1
    Also pretty easily write your own logging abstraction based on System.Diagnostics. I have experimented with Ukadc.Diagnostics and found it to be a very nice library if you are using System.Diagnostics as your logging system (and it makes using System.Diagnostics a reasonable choice). – wageoghe Oct 07 '11 at 17:51
0

According to this page

http://msdn.microsoft.com/en-us/library/a10k7w6c.aspx

TraceOptions Timestamp returns the number of ticks, so to convert ticks into time you need to do:

DateTime date = new DateTime(41329240725);
string FormattedDate =  date.ToShortDateString();

however 41329240725, seems a little small for ticks (I'm hoping that was just an example)

JonAlb
  • 1,702
  • 10
  • 14
  • actualy it as a real example. That number was written to the output textfile after the string (that was inserted int the TraceEvent). I want something like 10:22:34.32938. How shoud I configure the TextWriterTraceListener to achieve that? – Pedro77 Oct 07 '11 at 17:26