Share via


better formatting of date/timestamp for creating log file?

Question

Friday, May 14, 2010 9:24 PM

My current log file output lacks an understandable date/time format. It looks like

42:34:680

42:34:711

03:27:498

comboBoxLanguages_SelectedValueChanged Exception = DirectConnect Launch to Match file = E:\SETUP_TEST\Taxes.exe

How would I change the following (existing) code to give this an easily recognizable/understandable date and time format. Thank you.

System.IO.File.AppendAllText(System.IO.Path.Combine(ForAllUsers, "UIST_Log.txt"), "\n" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00") + ":" + DateTime.Now.Millisecond.ToString("000") + " " + message);

All replies (4)

Friday, May 14, 2010 9:35 PM ✅Answered | 1 vote

How about DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString()? It will give you something like this:

 Friday, May 14 2010 2:33:15 pm

There are a lot of options, for more options see http://msdn.microsoft.com/en-us/library/system.datetime_members.aspx and look at the ToXXXString() methods.


Friday, May 14, 2010 11:46 PM ✅Answered | 1 vote

I think using the UTC time would be a better idea. Especially when logging to database and not having to worry about formatting or spaces.

 Console.WriteLine(DateTime.Now.ToString("o"));
 Console.ReadLine();

 

produces

2010-05-14T18:44:32.7744276-05:00

 

My .NET Blog: http://michaelcrump.net


Friday, May 14, 2010 10:59 PM

Thank you Joe !

Perfect !


Sunday, May 16, 2010 1:13 PM

Thank you Michael! great idea. This may become important in my clients localized apps.