Reputation: 38643
I've got a very small standalone vb.net app that gets run automatically. Every now and then it hits an error condition that I want to log and then keep processing. But, this is far too minor a thing to store in the system's main log - I really just want to append a line to a text file.
What's the least stress way to append a line of text to a file (and have it create the file if it's not there) under .net?
Upvotes: 3
Views: 4035
Reputation: 416149
IO.File.AppendAllText(@"Y:\our\File\Name.here", "your log message here")
Upvotes: 19
Reputation: 175733
This is in C#, but should be trivial to change to VB:
void logMessage(string message)
{
string logFileName = "log.file";
File.AppendAllText(logFileName,message);
}
Edited because Joel's solution was much simpler than mine =)
Upvotes: 2
Reputation: 5696
Private Const LOG_FILE As String = "C:\Your\Log.file"
Private Sub AppendMessageToLog(ByVal message As String)
If Not File.Exists(LOG_FILE) Then
File.Create(LOG_FILE)
End If
Using writer As StreamWriter = File.AppendText(LOG_FILE)
writer.WriteLine(message)
End Using
End Sub
Upvotes: 2
Reputation: 85685
In VB.NET, My.Computer.FileSystem.WriteAllText will do the trick. If you don't like the My namespace, System.IO.File.AppendAllText works as well.
Upvotes: 3