Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Generate the Error Log

Posted by Viral Sarvaiya on September 10, 2009

This is the general thing is that, end user can not know the error is occur, means the error page will display when error is occur.
For that take a global.asax file, in this file just write the following code…


<%@ Application Language="VB" %>
<%@ import namespace="System.Web"%>
<%@ import namespace="System.Web.SessionState"%>
<%@ import namespace="System.IO"%>

<script runat="server">

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Try
'Code that runs when an unhandled error occurs

Dim currErr As Exception = Server.GetLastError()
Dim objErr As Exception = Server.GetLastError().GetBaseException()

Dim fileName As String = ConfigurationManager.AppSettings("SITE_ROOTPATH") & "ErrorLog/Error.log." & DateTime.Today.ToString("dd-MM-yyyy") & ".txt"

Dim LogFileStrm As StreamWriter
If (File.Exists(fileName)) Then
LogFileStrm = File.AppendText(fileName)
Else
LogFileStrm = File.CreateText(fileName)
End If
LogFileStrm.WriteLine("==============" & DateTime.Now.ToString("dd / MM / yyyy h:m tt") & "====================")
LogFileStrm.WriteLine("Message: " & currErr.Message)
LogFileStrm.WriteLine("Source: " & currErr.Source)
LogFileStrm.WriteLine("StackTrace: " & currErr.StackTrace)
LogFileStrm.WriteLine("TargetSite: " & currErr.TargetSite.ToString())
LogFileStrm.WriteLine("Error: " & currErr.ToString())
LogFileStrm.WriteLine("---------Base Error Info ")
LogFileStrm.WriteLine("-------------Message: " & objErr.Message)
LogFileStrm.WriteLine("-------------Source: " & objErr.Source)
LogFileStrm.WriteLine("-------------StackTrace: " & objErr.StackTrace)
LogFileStrm.WriteLine("-------------TargetSite: " & objErr.TargetSite.ToString())
LogFileStrm.WriteLine("-------------Error: " & objErr.ToString())
LogFileStrm.WriteLine("===========================================")
LogFileStrm.Flush()
LogFileStrm.Close()

'************* code added later
Dim strError As String = ""
strError = "==============" & DateTime.Now.ToString("dd / MM / yyyy h:m tt") & "====================<br>"
strError += "Message: " & objErr.Message & "<br>"
strError += "Source: " & objErr.Source & "<br>"
strError += "StackTrace: " & objErr.StackTrace & "<br>"
strError += "TargetSite: " & objErr.TargetSite.ToString() & "<br>"
strError += "Error: " & objErr.ToString() & "<br>"

''******************************
Dim errmsg As String = Server.UrlEncode(objErr.Message)
Response.Redirect("http://" & Request.ServerVariables("SERVER_NAME") & ":" & Request.ServerVariables("SERVER_PORT") & "/dmi/Error.aspx")

Server.ClearError()

Catch erx As Exception
Throw erx
End Try
End Sub

so this code is redirect the page to error page when error is occur.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: