John Charbonneau John Charbonneau - 1 month ago 13
Vb.net Question

Trouble with Date.Now.ToFileTime.ToString command (VB.NET)

I am trying to use the

Date.Now.ToFileTime.ToString
command in Visual Studio to assign the date and time to a text file name along with a suffix, however I am getting results such as the following:


131032106994631586log_detailed


Ideally what I would like to see is a file name of this type:


20160323_112154_log_detailed.txt


Here is my code so far:

Private Sub writeLog()

Using outfile As StreamWriter = New StreamWriter(Date.Now.ToFileTime.ToString + "log_detailled.txt", True)
outfile.Write(sb_detailled.ToString())
End Using

Using outfile As StreamWriter = New StreamWriter(Date.Now.ToFileTime.ToString + "log_Ends.txt", True)
outfile.Write(sb_ends.ToString())
End Using
End Sub

Answer

If you just want to make it work with the code you submited, it would be:

Private Sub writeLog()

    Using outfile As StreamWriter = New StreamWriter(String.Format("{0:yyyyMMdd_HHmmss}_log_detailed.txt", Now), True)
        outfile.Write(sb_detailled.ToString())
    End Using

    Using outfile As StreamWriter = New StreamWriter(String.Format("{0:yyyyMMdd_HHmmss}_log_Ends.txt", Now), True)
        outfile.Write(sb_ends.ToString())
    End Using

End Sub

...although maybe you would like to take a look into the library log4net: https://www.nuget.org/packages/log4net/ . I think it is highly likely that this library already does what you are trying to do (logging things).

It is largely used, although it is not that easy to have it up and running. This is a nice and detailed "how to": http://www.codeproject.com/Articles/899992/Logging-with-Microsoft-NET

Comments