Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have to add logs in my log files. To do that I have to have Date Time with micro seconds in the along with the Text.

The time stamp should in the format of HH:MM:SS:MilliSeconds:MicroSeconds

For Ex:
12:35:17:234:212 - The function to fetch data started.
12:35:17:234:451 - The function to fetch data Completed.
12:35:17:234:600 - The function to update data started.
12:35:17:234:754 - The function to update data is in progress.
12:35:17:234:900 - The function to update data completed.
12:35:17:235:002 - End of Process

Is there any way to handle this?

I tried to include logs like below but it didn't work. In all the log lines like this I'm getting the same output.

C#
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + (DateTime.Now.Ticks / 10).ToString() + " - The Function to fetch data started.");



Thanks & Regards,
Mathi.
Posted

In theory, you can use this to write out micro-seconds:
C#
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffffff"));
But, depending on your system hardware, and the time-cost of each logging operation, you may not be able to get microsecond timing accuracy; that's a problem of "resolution." See Jon Skeet's take on this which is the source of these comments: [^].

Fortunately, CodeProject is your friend and there are two good articles here on getting higher resolution timing accuracy: [^], [^].

One caution: do not use the standard WinForms Timer component if you need high accuracy: instead use one of the other Timers, like System.Timers.Timer.
 
Share this answer
 
v2
You can use "ffffff" in a format string to represent microseconds:

Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffffff"));
VB
To convert a number of ticks to microseconds, just use:

<pre lang="c#">long microseconds = ticks / (TimeSpan.TicksPerMillisecond / 1000);

If these don't help you, please provide more information about exactly what you're trying to do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900