Create a general Class:
================
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Globalization
Imports System.Net
Public Class clsErrHandler
''' Handles error by accepting the error message
''' Displays the page on which the error occured
Public Sub WriteError(ex As Exception)
'VARIABLE DECLARATION
Dim strErrorIn As String, strFileName As String, strErrorMethodName As String, strErrorMessage As String, _
strLineNo As String, strException As String, strDllInfo As String, strLocationInfo As String
Dim strFileCheck As String
Dim arrFileCheck As String()
Try
strException = ex.StackTrace.Trim().Remove(0, 3)
If strException.Contains(":") AndAlso strException.Contains(".") AndAlso strException.Contains("\") Then
strDllInfo = strException.Substring(0, strException.IndexOf("("c))
strLocationInfo = strException.Substring(strException.IndexOf(")"c), (strException.LastIndexOf(":"c)) - strException.IndexOf(")"c)).Remove(0, 5)
'Get the File Type & Name
strFileCheck = strLocationInfo.Substring(strLocationInfo.LastIndexOf("\"c)).Remove(0, 1)
arrFileCheck = strFileCheck.Split("."c)
If arrFileCheck.Length > 2 Then
strErrorIn = "Page File"
Else
strErrorIn = "Class File"
End If
strFileName = strFileCheck
'Get the Error Method Name
strErrorMethodName = strDllInfo.Substring(strDllInfo.LastIndexOf("."c)).Remove(0, 1)
'Get the Error Meassage
strErrorMessage = ex.Message
'Get the Error Line No.
strLineNo = strException.Substring(strException.LastIndexOf(":"c), (strException.Length - strException.LastIndexOf(":"c))).Remove(0, 6)
Else
strFileName = "-"
strErrorMethodName = ex.ToString()
strErrorIn = "Application Error"
strLineNo = "-"
strErrorMessage = ex.Message
End If
'Write the Error In ErroLog File
WriteToErrorLog(strErrorIn, strFileName, strErrorMethodName, strErrorMessage, strLineNo)
Catch exp As Exception
WriteError(exp)
End Try
End Sub
Public Sub WriteToErrorLog(strErrorIn As String, strFileName As String, strErrMethodName As String, strErrMessage As String, _
strLineNo As String)
'IP TRACKING START HERE
Dim strHostName As String = ""
Dim ipEntry As IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
Dim addr As IPAddress() = ipEntry.AddressList
Dim path As String = "~/ErrorLog/" & DateTime.Today.ToString("dd-MM-yyyy") & ".txt"
Try
strHostName = System.Net.Dns.GetHostName()
'IP TRACKING END HERE
'LOG FILE CREATE IF NOT EXISTS
If (Not File.Exists(System.Web.HttpContext.Current.Server.MapPath(path))) Then
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close()
End If
Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path))
w.WriteLine(Constants.vbCrLf & "Log Entry : ")
Dim err As String = "Error Path: " & System.Web.HttpContext.Current.Request.Url.ToString()
err = err & Environment.NewLine
err = err & "Error Date Time: " & DateTime.Now.ToString(CultureInfo.InvariantCulture)
err = err & Environment.NewLine
err = err & "Error Type: " & strErrorIn
err = err & Environment.NewLine
err = err & "File Name : " & strFileName
err = err & Environment.NewLine
err = err & "Error Method: " & strErrMethodName
err = err & Environment.NewLine
err = err & "Error Line: " & strLineNo
err = err & Environment.NewLine
err = err & "Error Message: " & strErrMessage
err = err & Environment.NewLine
err = err & "Host Name : " & strHostName & ". IP Address : " & addr(addr.Length - 1).ToString()
w.WriteLine(err)
w.WriteLine("__________________________________________________")
w.Flush()
w.Close()
End Using
Catch ex As Exception
WriteError(ex)
End Try
End Sub
End Class
Write in Catch
=========
Dim objErrHandler = New clsErrHandler()
Try
objErrHandler.WriteError(ex)
Catch exp As Exception
objErrHandler.WriteError(exp)
End Try
================
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Globalization
Imports System.Net
Public Class clsErrHandler
''' Handles error by accepting the error message
''' Displays the page on which the error occured
Public Sub WriteError(ex As Exception)
'VARIABLE DECLARATION
Dim strErrorIn As String, strFileName As String, strErrorMethodName As String, strErrorMessage As String, _
strLineNo As String, strException As String, strDllInfo As String, strLocationInfo As String
Dim strFileCheck As String
Dim arrFileCheck As String()
Try
strException = ex.StackTrace.Trim().Remove(0, 3)
If strException.Contains(":") AndAlso strException.Contains(".") AndAlso strException.Contains("\") Then
strDllInfo = strException.Substring(0, strException.IndexOf("("c))
strLocationInfo = strException.Substring(strException.IndexOf(")"c), (strException.LastIndexOf(":"c)) - strException.IndexOf(")"c)).Remove(0, 5)
'Get the File Type & Name
strFileCheck = strLocationInfo.Substring(strLocationInfo.LastIndexOf("\"c)).Remove(0, 1)
arrFileCheck = strFileCheck.Split("."c)
If arrFileCheck.Length > 2 Then
strErrorIn = "Page File"
Else
strErrorIn = "Class File"
End If
strFileName = strFileCheck
'Get the Error Method Name
strErrorMethodName = strDllInfo.Substring(strDllInfo.LastIndexOf("."c)).Remove(0, 1)
'Get the Error Meassage
strErrorMessage = ex.Message
'Get the Error Line No.
strLineNo = strException.Substring(strException.LastIndexOf(":"c), (strException.Length - strException.LastIndexOf(":"c))).Remove(0, 6)
Else
strFileName = "-"
strErrorMethodName = ex.ToString()
strErrorIn = "Application Error"
strLineNo = "-"
strErrorMessage = ex.Message
End If
'Write the Error In ErroLog File
WriteToErrorLog(strErrorIn, strFileName, strErrorMethodName, strErrorMessage, strLineNo)
Catch exp As Exception
WriteError(exp)
End Try
End Sub
Public Sub WriteToErrorLog(strErrorIn As String, strFileName As String, strErrMethodName As String, strErrMessage As String, _
strLineNo As String)
'IP TRACKING START HERE
Dim strHostName As String = ""
Dim ipEntry As IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
Dim addr As IPAddress() = ipEntry.AddressList
Dim path As String = "~/ErrorLog/" & DateTime.Today.ToString("dd-MM-yyyy") & ".txt"
Try
strHostName = System.Net.Dns.GetHostName()
'IP TRACKING END HERE
'LOG FILE CREATE IF NOT EXISTS
If (Not File.Exists(System.Web.HttpContext.Current.Server.MapPath(path))) Then
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close()
End If
Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path))
w.WriteLine(Constants.vbCrLf & "Log Entry : ")
Dim err As String = "Error Path: " & System.Web.HttpContext.Current.Request.Url.ToString()
err = err & Environment.NewLine
err = err & "Error Date Time: " & DateTime.Now.ToString(CultureInfo.InvariantCulture)
err = err & Environment.NewLine
err = err & "Error Type: " & strErrorIn
err = err & Environment.NewLine
err = err & "File Name : " & strFileName
err = err & Environment.NewLine
err = err & "Error Method: " & strErrMethodName
err = err & Environment.NewLine
err = err & "Error Line: " & strLineNo
err = err & Environment.NewLine
err = err & "Error Message: " & strErrMessage
err = err & Environment.NewLine
err = err & "Host Name : " & strHostName & ". IP Address : " & addr(addr.Length - 1).ToString()
w.WriteLine(err)
w.WriteLine("__________________________________________________")
w.Flush()
w.Close()
End Using
Catch ex As Exception
WriteError(ex)
End Try
End Sub
End Class
Write in Catch
=========
Dim objErrHandler = New clsErrHandler()
Try
objErrHandler.WriteError(ex)
Catch exp As Exception
objErrHandler.WriteError(exp)
End Try
No comments:
Post a Comment