1 Public Shared Sub WriteLog(ByVal FormID As String, ByVal ex1 As Exception) 2 'log信息 3 Dim loginfo As String = "" 4 5 '获取现在的时间 6 Dim strDate As String 7 strDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 8 9 '获取画面ID 10 Dim strFormId As String 11 strFormId = FormID 12 13 '获取路径 14 Dim path As String 15 path = Application.StartupPath.ToString.Split(":")(0).ToString() & ": " & "REMSLOG" 16 17 If Not My.Computer.FileSystem.DirectoryExists(path) Then 18 My.Computer.FileSystem.CreateDirectory(path) 19 End If 20 21 path &= "Err.log" 22 23 loginfo = strDate + System.Environment.NewLine + strFormId + ":" 24 25 Dim SW As System.IO.StreamWriter = Nothing 26 Try 27 SW = New System.IO.StreamWriter(path, True) 28 SW.WriteLine(loginfo) 29 SW.WriteLine("Err Info:" & ex1.Message) 30 SW.WriteLine("Err Source:" & ex1.StackTrace) 31 SW.WriteLine("Err Function:" & ex1.TargetSite.Name) 32 Catch ex As Exception 33 Finally 34 If Not SW Is Nothing Then 35 SW.Flush() 36 SW.Close() 37 End If 38 End Try 39 End Sub
Try--Catch中直接调用该方法即可。