Introduction:
After deploying your web application you have to know about errors that occur when somebody (any "user") uses the application. Usually, you need to know real situation (real error message, source etc.) and to send to the client only some short message. One of convenient methods to make it is to mail to you the real error message and to redirect the user (client) to some special error page. In this article I will describe the error handler for ASP.NET 2.0 applications that is developed in a separate class library project. You can use it in any web application you want by adding a reference to the compiled dll assembly without writing many lines of code. The examples are written using VB.Net.
In the project, named ErrorHandler and having type of the "Class Library", we create the class WebErrorHandler. Among other properties of the class there are such properties as: C_MailAddressTo, which set or get all emails where the error message has to be sent to (your email, email of your manager, email of the manager of your manager etc.); C_MailSubject, which set or get the subject of the mail with default value "Error Message"; C_Host, which set or get the domain name; C_RedirectPath, which set or get the URL of the web page for the error messages. The class has the method doErrorHandler(), which forms the error message, sends this message according to the C_MailAddressTo property and redirects the user to the special error page (according to the C_RedirectPath property).
The C_RedirectPath property has one small feature. If the string (C_RedirectPath) contains "?" (For example: "ErrorForm.aspx? ErrorMessage=" ), we have possibility to receive the "short error message" on our error page. Saying "short error message", I just mean the first sentense of the whole error message. It can be very useful when we want not only to inform about some error (like: "An error has occurred!"), but to add some information about the kind of the error.
For example: suppose we have the message, "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (Provider: " and so on.)
In this case the "short error message" is: "An error has occurred while establishing a connection to the server".
The code of the WebErrorHandler class is the following:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Net.Mail
Imports System.Web
Imports System.Web.UI
namespaceErrorHandler
Public Class WebErrorHandler
#Region "forClass"
private List(Of String) _MailAddressTo
private String _MailSubject = "Error Message"
private String _MailAddressFrom = ""
private String _RedirectPath = "ErrorHandler.aspx"
private String _Host=""
private HttpResponse _Response
private HttpServerUtility _Server
#End Region
#Region "Properties"
public List(Of String) C_MailAddressTo
Get
Return _MailAddressTo
End Get
Set
_MailAddressTo = value
End Set
public String C_MailSubject
Get
Return _MailSubject
End Get
Set
_MailSubject= value
End Set
public String C_MailAddressFrom
Get
Return _MailAddressFrom
End Get
Set
_MailAddressFrom = value
End Set
public String C_RedirectPath
Get
Return _RedirectPath
End Get
Set
_RedirectPath = value
End Set
public String C_Host
Get
Return _Host
End Get
Set
_Host = value
End Set
public HttpResponse C_Response
Set
_Response = value
End Set
public HttpServerUtility C_Server
Set
_Server = value
End Set
#End Region
Public Sub doErrorHandler()
Dim exc As Exception = _Server.GetLastError().GetBaseException()
Dim sErrorMessage As String = "<br/><b style ='color:Red'>MESSAGE:</b><br/>" & exc.Message & "<br/><b style ='color:Red'>SOURCE:</b><br/>" & exc.Source & "<br/><b style ='color:Red'>TARGETSITE:</b><br/>" & exc.TargetSite & "<br/><b style ='color:Red'>StackTrace:</b><br/>" & exc.StackTrace & "<br/><b style ='color:Red'>Data:</b><br/>" & exc.Data & "<br/><b style ='color:Red'>InnerException:</b><br/>" & exc.InnerException
Dim sErrorMessageShort As String = exc.Message.Substring(0,exc.Message.IndexOf("."))
Dim messageMail As MailMessage = New MailMessage()
Dim clientMail As SmtpClient = New SmtpClient()
If _MailAddressFrom.Trim() <> "" Then
messageMail.From = New MailAddress(_MailAddressFrom)
End If
For Each addressTo As String In _MailAddressTo
messageMail.To.Add(New MailAddress(addressTo))
Next addressTo
messageMail.Subject = _MailSubject
messageMail.Body = sErrorMessage
messageMail.IsBodyHtml = True
If _Host.Trim() <> "" Then
clientMail.Host = _Host
End If
clientMail.Send(messageMail)
If _RedirectPath.Contains("?") Then
_Response.Redirect(_RedirectPath & sErrorMessageShort)
Else
_Response.Redirect(_RedirectPath)
End If
_Server.ClearError()
End Sub
End Class
End Namespace
After building our project you can use it in any web application you want by adding a reference to the compiled ErrorHandler.dll assembly. In order to handle error we will use the Application_Error method of the Global.asax file:
Private Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ErrHandler As ErrorHandler.WebErrorHandler = New ErrorHandler.WebErrorHandler()
Dim MailTo As List(Of String) = New List(Of String)()
MailTo.Add("My_Email@email").
MailTo.Add("MyManager_Email@email")
MailTo.Add("MyManagerManager_Email@email")
ErrHandler.C_Host = "MyHost" 'or from Web.Config
ErrHandler.C_MailAddressFrom = "fromError@mail" 'or from Web.Config
ErrHandler.C_MailAddressTo = MailTo
ErrHandler.C_MailSubject = "One more Error Message" 'or used default
'ErrHandler.C_RedirectPath =
' "~/Forms/ErrorForm.aspx";//without message
ErrHandler.C_RedirectPath = "~/Forms/ErrorForm.aspx?ErrorMessage="
ErrHandler.C_Response = Response
ErrHandler.C_Server = Server
ErrHandler.doErrorHandler()
End Sub
If you want to define the "Host" and "Mail from" in the Web.Config file you do not need to set the properties C_Host and C_MailAddressFrom in the Application_Error method, you just have to add inside of the "configuration" tag of the Web.Config the following code:
<system.net>
<mailSettings>
<smtp from="fromError@mail">
<network host="MyHost"/>
</smtp>
</mailSettings>
</system.net>
In order to test our ErrorHandler, just add to your project some ErrorPage.aspx with at least one label (For example: LabelErrorMessage). Add to Page_Load method the following code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Request.QueryString("ErrorMessage") Is Nothing AndAlso
Request.QueryString("ErrorMessage").Trim ()<>""
Then
LabelErrorMessage.Text = Request.QueryString("ErrorMessage")
End If
End Sub
Now "spoil" your connection string and run the application. You (your manager, etc.) will receive the email like this (Fig. 1):

Figure 1:
The user will be redirect to the ErrorPage like this (fig.2):

Figure 2:
CONCLUSION:
I hope, that this article will help you to create your own ErrorHandler.dll with your own requirements, which can help to standardize and develop your web application.
Good luck in programming!
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C-SHARPCORNER (http://www.c-sharpcorner.com/).