c# - 以毫秒为单位创建日志

标签 c#

我在日志中编写一个程序的事件,每行都附有时间戳,格式如下:

DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss FFF")

我原以为毫秒部分总是有 3 位数字。但是出现以下内容:

11/29/2013 18:53:14 96 -  SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - Begin
11/29/2013 18:53:14 975 -  SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - End

第一个为什么毫秒部分不是096?

最佳答案

来自 "FFF" Custom Format Specifier

The "FFF" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. However, trailing zeros or three zero digits are not displayed.

编辑:好的,我想我明白这里发生了什么。原来,OP的毫秒部分是960不是096 .

这就是为什么它完全按照 MSDN 页面中的描述工作。

尾随0960 的末尾, 它不领先 0096 .

作为解决方案,您可以使用 The "fff" Custom Format Specifier 反而。在我看来,它不尾随或前导零。至少它没有写在MSDN 页面:)

EDIT2:我认为人们混淆了 trailing zero 是什么和 leading zero

来自维基百科页面;

Trailing zero

In mathematics, trailing zeros are a sequence of 0s in the decimal representation (or more generally, in any positional representation) of a number, after which no other digits follow.


Leading zero

A leading zero is any 0 digit that leads a number string in positional notation. For example, James Bond's famous identifier, 007, has two leading zeros.

关于c# - 以毫秒为单位创建日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287851/

相关文章:

c# - 变量 'x' 已声明但从未使用错误

c# - 通过匿名方法分配匿名类型的属性

c# - SignalR SQLTableDependency 数据库更改跟踪问题

c# - 为什么我的 C# 类函数返回显示红色波浪线?

c# - Nuget 包命名空间与原始代码不同

c# - 如何比较字符串和 bool 数组?

c# - 当调用委托(delegate)时,如何发送用作参数的参数列表?

c# - 团结 : is it possible to configure the scene for trigger colliders to only work with one layer?

c# - 使用Partial Classes来管理代码,好的解决方案?

c# - 将 Windows Azure 项目升级到 2.2