How to pass exception aspx location to error class

I have written an exception class to log my exceptions and created a cookie to store the name of the page that the exception originated from and then displaying it in the errors redirect page.  Is there a simpler way to accomplish this.  Can I create the cookie or some other method to store the page name containing the exceptionr in the exceptionr class.  I have tried but can't get this to work.  What I have done does work but I am not sure sure this is the best way to handle this.  Any advice would be appreciated.  Thank-you.
'CODE IN THE ASPX PAGE TO HANDLE EXCEPTIONS
            Catch ex As Exception
            Dim myerror As ErrorsClass
            myerror = New ErrorsClass(ex.Message, ex.Source, ex.StackTrace)
'CREATE COOKIE
            System.Web.HttpContext.Current.Response.Cookies("PageFrom").Value = Request.ServerVariables("url")
            myerror.writererror()
            Throw
            Finally
 
'CODE IN THE ERRORS CLASS 
Public Class ErrorsClass
    'declare Message, Source and StackTrace
    Inherits Exception
    Private _exceptionMessage As String
    Private _exceptionSource As String
    Private _exceptionStackTrace As String
    'create overrideable Readonly property for exception message
    Public Overridable ReadOnly Property exceptionMessage() As String
        Get
            Return _exceptionMessage
        End Get
    End Property
    'pass message, source and stacktrace when new errors class is created
    Public Sub New(ByVal Message As String, ByVal Source As String, ByVal StackTrace As String)
        MyBase.New(Message)
        _exceptionMessage = Message
        _exceptionSource = Source
        _exceptionStackTrace = StackTrace
    End Sub
    'write error to text file
    Public Sub writererror()
        Dim file As New System.IO.StreamWriter(HttpContext.Current.Server.MapPath("..\ErrorLog.txt"), True)
        file.WriteLine(FormatDateTime(Date.Now, DateFormat.GeneralDate))
        file.WriteLine("Exception Source= " & _exceptionSource)
        file.WriteLine("Exception Message= " & _exceptionMessage)
        file.WriteLine("Exception StackTrace= " & _exceptionStackTrace)
        file.Close()
        System.Web.HttpContext.Current.Response.Redirect("http:../ErrorsRedirectPage.aspx")
    End Sub
End Class
 
'CODE IN ERRORS REDIRECT PAGE
<form id="Form1" style="FONT-SIZE: 11pt; FONT-FAMILY: Arial" method="post" runat="server">
An error has occurred on 
<%Response.write(Request.Cookies("PageFrom").value)%>. Please contact the Helpdesk at local 6706.<br>
			<br>
			<A href="NavigationNET.aspx">Online Forms Home Page</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A href="NavigationNET.aspx?Status=CANCEL">Cancel</A>
		</form>

Open in new window

lnshopAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
samtran0331Connect With a Mentor Commented:
are you using the customErrors setting in web.config?
If yes, then .Net will automatically redirect to the error page when an error occurs, and automatically add a querystring variable named "ErrorPage" to the error page.
This is an extremely simple example of using an html page as an error page, and still using the querystring variable set by .Net
http://samples.gotdotnet.com/quickstart/aspplus/doc/handlingerrs.aspx

if instead of an html page, you set the webconfig to use your ErrorsRedirectPage.aspx page, you can instantiate and use your class on that page.  that way, you don't have to instantiate your error class in every try/catch...
or another common approach is to use the global.asax page to instantiate and handle your error class...
http://www.developer.com/net/asp/article.php/961301
If you look at listing 2, it does some of what your class does...but the key is that you can manage all errors using global.asax, and not have to have it in every try/catch
0
 
lnshopAuthor Commented:
Thank-you very much for your help.  I need to log the errors to a text file so using creating a custom class seemed to be the solution.  I have tried the default redirect and could not get this to work.  I am only using the try catch in pages that contain database connections so it was not necessary to use the the global asax.
0
All Courses

From novice to tech pro — start learning today.