Public Class ErrParser
Public Shared Function GetProcName(ByVal ErrStackTrace As String) As String
Dim ExeName As String
Dim LastSlashLoc As Integer
Dim NameLoc As Long
Dim TempStr As String
Dim ParenLoc As Integer
ExeName = System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
ExeName = Replace(ExeName, " ", "_")
NameLoc = ErrStackTrace.IndexOf(ExeName)
TempStr = Mid(ErrStackTrace, NameLoc + Len(ExeName) + 2)
ParenLoc = TempStr.IndexOf("(")
GetProcName = Mid(TempStr, 1, ParenLoc)
End Function
Public Shared Function GetErrLineNum(ByVal ErrStackTrace As String) As String
Dim LineLoc As Long
LineLoc = ErrStackTrace.LastIndexOf(":line")
GetErrLineNum = Mid(ErrStackTrace, LineLoc + 6)
End Function
Public Shared Sub LogError(ByVal ErrMsg As String)
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(Application.StartupPath & "\DMS_Error_Log.log", True)
sw.WriteLine("****************************************************************************")
sw.WriteLine(Date.Now.ToString)
sw.WriteLine(ErrMsg)
sw.Close()
Catch ex As Exception
End Try
End Sub
End Class
'Usage Template
'Dim ep As New ErrParser
'Try
' Some Code Here
'Catch ex As Exception
' Dim ErrMsg As String
' ErrMsg = "Error in " & ep.GetProcName(ex.StackTrace)
'#If DEBUG Then
' ErrMsg &= " in Line " & ep.GetErrLineNum(ex.StackTrace)
'#End If
' ErrMsg &= vbCrLf
' ErrMsg = ErrMsg & "Error Message: " & ex.Message
' MsgBox(ErrMsg, MsgBoxStyle.Exclamation Or MsgBoxStyle.OKOnly, "Message Box Title Here")
'End Try