Tran
Tran

Reputation: 179

Write exception in text file application path

I would like to catch exceptions to my application startup path in a file called "log_errors" and new exceptions on a new line, instead of messagebox.show:

Try
    Dim t As Thread = New Thread(New ThreadStart(AddressOf MySub))
    _runningThreads.Add(t)
    t.Start()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

Upvotes: 0

Views: 227

Answers (1)

J. Scott Elblein
J. Scott Elblein

Reputation: 4283

Answering your specific question, for a very basic logging feature:

Create a "logging" Sub and pass the message to it:

Public Sub LogToFile(ByVal strMessage as String)

  ' Will create a log_errors.txt file if it doesn't already exist; otherwise, appends to it.
  File.AppendAllText(Application.StartupPath & "\log_errors.txt", strMessage)

End Sub

Then in your Catch ex As Exception, instead of:

MessageBox.Show(ex.Message)

do

LogToFile(ex.Message)

Upvotes: 1

Related Questions