Error Log in Asp.net 2.0

Ashish
Posted by Ashish under ASP.NET category on | Views : 4760
Errors may occur during development of a website.So to trace and fix the error in Asp.net 2.0 we used error handling mechanisms.

Small Project for Tracing and Maintaning Logg File in Project.

------------------------------------------------------------------
Step 1 : Create new Folder and name it ErrorLogFile.
Right Click on website > New folder (Rename ErrorLogFile)

------------------------------------------------------------------------------
Step 2 : Crete Class File in App_Code and name it ErrorHandler.vb

ErrorHandler.vb

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Globalization

Public Class ErrorHandler
Public Shared Sub WriteError(ByVal errorMessage As String)
Try
Dim path As String = "~/ErrorLogFile/" & DateTime.Today.ToString("dd-mm-yy") & ".txt"
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 : ")
w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture))
Dim err As String = "Error in: " & System.Web.HttpContext.Current.Request.Url.ToString() & ". Error Message:" & errorMessage
w.WriteLine(err)
w.WriteLine("__________________________")
w.Flush()
w.Close()
End Using
Catch ex As Exception
WriteError(ex.Message)
End Try

End Sub


---------------------------------------------------------------------------
Step 3: Create Homepage.aspx and Maintain Page level error.

Partial Class HomePage
Inherits System.Web.UI.Page
Dim EHandler As new ErrorHandler
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Throw New Exception()
Catch ex As Exception
EHandler.WriteError(ex.Message)

End Try
End Sub
End Class

Comments or Responses

Login to post response