This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Sunday, November 2, 2008

ASP.NET Global Error Handler (with html report & email functionality)



Globally capture errors and exceptions in your ASP.NET site. Admin(s) can be emailed a detailed HTML report of the error, then redirect the user to a friendly error page. 100% C#, no external objects needed.

CODE :

//**************************************
// for :ASP.NET Global Error Handler (with html report & email functionality)
//**************************************
Copyright 2003 Joel Thoms
//**************************************
// Name: ASP.NET Global Error Handler (with html report & email functionality)
// Description:Globally capture errors and exceptions in your ASP.NET site. Admin(s) can be emailed a detailed HTML report of the error, then redirect the user to a friendly error page. 100% C#, no external objects needed.
// By: Joel Thoms
//
//
// Inputs:None
//
// Returns:None
//
//Assumes:None
//
//Side Effects:None
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.948/lngWId.10/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

/* Author: Joel Thoms
* Website: http://www.joel.net
* Email: (contact me through website)
* Date: 02.05.2003
*
* Copyright 2003 Joel Thoms
*
* Description:
* HtmlError generates an HTML error message from the generated Exception. HtmlError also includes
* a routine to email the error message to the admin(s).
*
* This object can be used to capture individual errors, though it's best use is to globally capture
* errors using Global.asax. Both examples are provided.
*
*
* Usage and Examples:
*
* Here is an example on how to capture a simple division by zero error.
*
* [C#]
* // Division by zero error example
* try {
* int x = 0;
* x = 1 / x;
* } catch (Exception Ex) {
* // Display Error Message to the browser
* Response.Write(HtmlError.getHtmlError(Ex));
*
* // Don't Specify SMTP Server
* HtmlError.sendHtmlError(Ex, "YOUR-EMAIL@ADDRESS.COM");
*
* // Specify SMTP Server
* //HtmlError.sendHtmlError(Ex, "YOUR-EMAIL@ADDRESS.COM", "your.smtp-server.com");
* }
*
*
* [VB.NET]
*
* ' Division by zero error example
* Try
* Dim x As Integer = 0
* x = 1 / x
* Catch Ex As Exception
* ' Display Error Message to the browser
* Response.Write(HtmlError.getHtmlError(Ex))
*
* ' Don't Specify SMTP Server
* HtmlError.sendHtmlError(Ex, "YOUR-EMAIL@ADDRESS.COM")
*
* ' Specify SMTP Server
* 'HtmlError.sendHtmlError(Ex, "YOUR-EMAIL@ADDRESS.COM", "your.smtp-server.com")
* End Try
*
*
* Here is an example on globally capturing errors using the Global.asax file.
*
* [C#]
*
* protected void Application_Error(Object sender, EventArgs e) {
* // Don't Specify SMTP Server
* HtmlError.sendHtmlError(Context.Error.GetBaseException(), "YOUR-EMAIL@ADDRESS.COM");
*
* // Specify SMTP Server
* //HtmlError.sendHtmlError(Context.Error.GetBaseException(), "YOUR-EMAIL@ADDRESS.COM", "your.smtp-server.com");
*
* // Redirect User to Friendly Error Page
* Response.Redirect("/error.aspx");
* }
*
* [VB.NET]
* Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
* ' Don't Specify SMTP Server
* HtmlError.sendHtmlError(Context.Error.GetBaseException(), "YOUR-EMAIL@ADDRESS.COM")
*
* ' Specify SMTP Server
* 'HtmlError.sendHtmlError(Context.Error.GetBaseException(), "YOUR-EMAIL@ADDRESS.COM", "your.smtp-server.com")
*
* ' Redirect User to Friendly Error Page
* Response.Redirect("/error.aspx")
* End Sub
*
*
*/
using System;
using System.Data;
using System.Web;
using System.Web.Mail;
using System.Collections.Specialized;
///

HtmlError Object.
public class HtmlError {
public HtmlError() { }
static public void sendHtmlError(Exception Ex, string email_address) { sendHtmlError(Ex, email_address, ""); }
static public void sendHtmlError(Exception Ex, string email_address, string smtp_server) {
MailMessage mail = new MailMessage();
mail.From = "server-errors@discountasp.net";
mail.To = email_address;
mail.Subject = "Uncaptured Error";
mail.Body = getHtmlError(Ex);
mail.BodyFormat = MailFormat.Html;
if (smtp_server.Length > 0) SmtpMail.SmtpServer = smtp_server;
SmtpMail.Send(mail);
}
/// Returns HTML an formatted error message.
static public string getHtmlError(Exception Ex) {
// Heading Template
const string heading = "
 
";
// Error Message Header
string html = "Error - " + Ex.Message + "

";
// Populate Error Information Collection
NameValueCollection error_info = new NameValueCollection();
error_info.Add("Message", cleanHTML(Ex.Message));
error_info.Add("Source", cleanHTML(Ex.Source));
error_info.Add("TargetSite", cleanHTML(Ex.TargetSite.ToString()));
error_info.Add("StackTrace", cleanHTML(Ex.StackTrace));
// Error Information
html += heading.Replace("", "Error Information");
html += CollectionToHtmlTable(error_info);
// QueryString Collection
html += "

" + heading.Replace("", "QueryString Collection");
html += CollectionToHtmlTable(HttpContext.Current.Request.QueryString);

// Form Collection
html += "

" + heading.Replace("", "Form Collection");
html += CollectionToHtmlTable(HttpContext.Current.Request.Form);
// Cookies Collection
html += "

" + heading.Replace("", "Cookies Collection");
html += CollectionToHtmlTable(HttpContext.Current.Request.Cookies);
// Session Variables
html += "

" + heading.Replace("", "Session Variables");
html += CollectionToHtmlTable(HttpContext.Current.Session);
// Server Variables
html += "

" + heading.Replace("", "Server Variables");
html += CollectionToHtmlTable(HttpContext.Current.Request.ServerVariables);
return html;
}
static private string CollectionToHtmlTable(NameValueCollection collection) {
// ... Template
const string TD = "";
// Table Header
string html = "\n\n"
+ " " + TD.Replace("", " Name")
+ " " + TD.Replace("", " Value") + "\n";
// No Body? -> N/A
if (collection.Count == 0) {
collection = new NameValueCollection();
collection.Add("N/A", "");
}
// Table Body
for (int i = 0; i < collection.Count; i++) {
html += ""
+ TD.Replace("", collection.Keys[i]) + "\n"
+ TD.Replace("", collection[i]) + "\n";
}
// Table Footer
return html + "
";
}
static private string CollectionToHtmlTable(HttpCookieCollection collection) {
// Overload for HttpCookieCollection collection.
// Converts HttpCookieCollection to NameValueCollection
NameValueCollection NVC = new NameValueCollection();
foreach (string item in collection) NVC.Add(item, collection[item].Value);
return CollectionToHtmlTable(NVC);
}
static private string CollectionToHtmlTable(System.Web.SessionState.HttpSessionState collection) {
// Overload for HttpSessionState collection.
// Converts HttpSessionState to NameValueCollection
NameValueCollection NVC = new NameValueCollection();
foreach (string item in collection) NVC.Add(item, collection[item].ToString());
return CollectionToHtmlTable(NVC);
}
static private string cleanHTML(string Html) {
// Cleans the string for HTML friendly display
return (Html.Length == 0) ? "" : Html.Replace("<", "<").Replace("\r\n", "
").Replace("&", "&").Replace(" ", " ");
}
}

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger