开发者

How to format the Timestamp data when using TraceOptions.Timestamp

How can I format the Timestamp data that is prin开发者_运维技巧ted 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?


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?


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜