I made a project in VB.NET. If the application I made will produce an unwanted error, it will create a text file containing the error. I was able to do this when running it on Visual Studio but it does not work when running the separate application, the executable file found on bin/Debug.
Here's what I've done:
Sub Main(ByVal ParamArray args() As String)
Try
System.Windows.Forms.Application.Run(New Form1)
Catch ex As Exception
WriteErrorLogs(ex)
End Try
End Sub
Sub WriteErrorLogs(Byval ex As Exception)
' create a textfile the write x.Message, x.Source, x.ToString
Dim dnow As String = Now.ToString
Dim filename As String = "Error " & removeInvalidChars(dnow)
Dim saveto As String = New IO.FileInfo("Errors/" & filename).FullName & ".txt"
Dim title As String = ex.Message
Dim stacktrce As String = ex.StackTrace
If Not IO.Directory.Exists(New IO.DirectoryInfo("Errors").FullName) Then IO.Directory.CreateDirectory("Errors")
Dim fw As New IO.StreamWriter(saveto, False, System.Text.Encoding.UTF8)
fw.WriteLine(title)
fw.WriteLine()
fw.WriteLine(stacktrce)
fw.Close()
End Sub
Private Function removeInvalidChars(ByRef s As String)
Dim invalidChars() As Char = "\/:*?""<>|".ToCharArray
For Each i As Char In invalidChars
s = s.Replace(i, ".")
Next
Return s
End Function
Is there a better solution for this?