使用 TraceOptions.Timestamp 时如何格式化时间戳数据

发布于 2024-12-08 11:31:49 字数 197 浏览 4 评论 0原文

当设置 TraceOutputOptions = TraceOptions.Timestamp 时,如何格式化打印的时间戳数据?

我得到类似的东西: 时间戳=41329240725(写入输出文本文件的实际值)

编辑: 我想要类似 10:22:34.32938 的内容。我应该如何配置 TextWriterTraceListener 来实现这一目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2024-12-15 11:31:50

您真的想记录消息的写入时间吗?如果是这样,您需要使用 TraceOptions.DateTime。请注意,根据 MSDN,时间写为 UTC。

如果您想要更多地控制时间的格式(包括如果您希望它以 UTC 以外的其他方式表示),那么您可能会编写自己的自定义 TraceListener,或者找到一个可以执行您想要的操作的 TraceListener。

System.Diagnostics 的一个有用插件是 Ukadc.Diagnostics。使用它,您可以轻松地向日志消息添加自定义格式(类似于使用 log4net 和 NLog 所做的事情)。

以下是我过去提供的一些其他答案链接,用于记录您可能会发现有用的问题:

我什么时候应该使用跟踪与 Logger.NET、企业库、 log4net 或 Ukadc.Diagnostics?

我什么时候在代码中需要多个 TraceSource?

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?

凤舞天涯 2024-12-15 11:31:50

根据此页面

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

TraceOptions Timestamp 返回刻度数,因此要将刻度数转换为时间,您需要执行以下操作:

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

但是 41329240725 似乎是对于蜱虫来说有点小(我希望这只是一个例子)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文