Error Logging Class


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Public Class ERROR_LOGGING
  2.  
  3. Public Shared Sub writeError(ByVal sPathName As String, ByVal sErrMsg As String)
  4.  
  5. Dim LogFormat As String = DateTime.Now.ToShortDateString & " " & DateTime.Now.ToLongTimeString & " ==> "
  6. Dim year As String = DateTime.Now.Year.ToString
  7. Dim month As String = DateTime.Now.Month.ToString
  8. Dim day As String = DateTime.Now.Day.ToString
  9. Dim ErrorTime As String = month + "_" + day + "_" + year
  10.  
  11. Dim fileName As String = sPathName + ErrorTime + ".log" 'Server.MapPath("TestMsg.txt")
  12.  
  13. IO.File.AppendAllText(fileName, LogFormat + sErrMsg + ControlChars.NewLine + ControlChars.NewLine)
  14.  
  15. End Sub
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. End Class
  23.  
  24.  
  25.  
  26. ' Example on its usage I use it in the global.asx file
  27.  
  28. Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
  29. ' Code that runs when an unhandled error occurs
  30. Try
  31. ERROR_LOGGING.writeError(Server.MapPath("~/Logs/ErrorLog/"), Server.GetLastError.Message + Server.GetLastError.InnerException.ToString)
  32. Response.Redirect("~/security/index.aspx")
  33. Catch ex As Exception
  34.  
  35. End Try
  36. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.