Website Error Reporter (Smart way to handle website errors) in ASP.NET


Introduction

In this article and video post, you will learn how to develop such web application in ASP.NET which can report to the Admin guy about the errors that end users (website users) is experiencing on your blog/website. After reading this post you are definitely going to add this feature to your existing website or blog.


I know I'm bit fogy in programming, but I write whatever I experience or learn. Within last 2-3 days I experienced errors on very-very popular websites namely Google, Yahoo and even few technical community websites. I can't doubt on Google or Yahoo because they have very highly qualified system to deal with.





But I can doubt on websites which redirect me on the page having following message:


Displayed error page "Server Error in '/' Application.", means they are not dealing properly with errors. When I reported to the admin guys, they again asked me back to provide the source URL and target URL, where I saw this error. Even in past I was asking the same from my blog (www.itorian.com) users to provide the broken links or some other bugs. So, it is normal thing in web apps.

Developing bug free website is desired by every Web Technologist. We can't fix our 100% bugs from our applications especially from web platform, because on web platform we not only program code-behind, databases and views even managing each and every URL's on website is also big task.

Well, I experience this so, I'm going to write the same post which describes how to deal.


Now, follow the steps (described in video) to create such Web application.

Step1

Create Web Application by navigating File > New > Project > ASP.NET Web Application.

Step2

Open the Global.asax file and type following coding in Application_Error.

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    Response.Write("<h2>Error Page</h2>");
       
    // Get the exception details
    Exception exc = Server.GetLastError();
    Response.Write("Sorry for the inconvenience that may have been caused to you.");
    Response.Write("<b>Error Message</b><p>" + exc.Message + "</p><br>");
       
    // Clear the error from the server
    Server.ClearError();

    // Report to Administrator
    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    string MailBody = "Website user is getting bad experience on you website. Find the details below:<br><br>";
    MailBody = MailBody + "<br><br><b>Error on Page (Target Webpage): </b>" + url;
    MailBody = MailBody + "<br><br><b>Link on Webpage (Source Webpage): </b>" + Request.UrlReferrer;
    MailBody = MailBody + "<br><br><b>Error Message: </b>" + exc.Message;
    MailBody = MailBody + "<br><br><b>Trace Message: </b>" + exc.StackTrace;
       
    // Other browser based stuffs
    MailBody = MailBody + "<br><br><b>Platform: </b>" + Request.Browser.Platform;
    MailBody = MailBody + "<br><br><b>Browser: </b>" + Request.Browser.Browser;
    MailBody = MailBody + "<br><br><b>Browser Type: </b>" + Request.Browser.Type;
    MailBody = MailBody + "<br><br><b>Browser Version: </b>" + Request.Browser.Version;
       
    string MailResponse = sendmail("[email protected]", "Bad Experience", MailBody);
    Response.Write("<br><br><b>Note: </b>" + MailResponse.ToString());
}

Step3

Create a new method in Global.asax file for sendmail method as given in above code. Here is the method code:

private string sendmail(string ToEmailAdd, string MailSubject, string MailMessageHTMLString)
{
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

    message.To.Add(new System.Net.Mail.MailAddress(ToEmailAdd));
    message.From = new System.Net.Mail.MailAddress("[email protected]", "ITORIAN.COM");
    message.Subject = MailSubject;
    message.Body = MailMessageHTMLString;
    message.IsBodyHtml = true;

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

    client.Port = 25;
    client.Host = "mail.itorian.com";
    System.Net.NetworkCredential nc = new System.Net.NetworkCredential("[email protected]", "*****");
    client.UseDefaultCredentials = false;
    client.Credentials = nc;

    try
    {
        client.Send(message);
        return "Automated System success to report this to the Administrator.";
    }
    catch
    {
        return "Automated System not success to report this to the Administrator.";
    }
}

Step4

Now open the Site.Master and create any broken link or any other runtime mistake and then run the project.

Step5

Type to navigate by clicking broken link, it will display the error message instead of displaying the red page error and also sends the email to you inbox containing full error description in body.
Video Post


Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System