Tuesday, May 15, 2012

Exception Handling using Global.asax in asp.net


<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>

<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        Exception err = Server.GetLastError();
       
        Session["error"] = err.InnerException.Message;
        Session["ErrorMessage"] = err.InnerException.Message;
        Session["Message"] = err.Message.ToString();
        Session["ExceptionType"] = err.GetType().ToString();

        Session["Source"] = err.Source.ToString();
        Session["TargetSite"] = err.TargetSite.ToString();

        Session["Timestamp"] = DateTime.Now.ToString();

        if (err is HttpException && err.InnerException != null)
        {

            err = err.InnerException; //Avoid the Unhandled Exception wrapper which is meaningless

        }

        MailMessage msg = new MailMessage();
        HttpContext ctx = HttpContext.Current;

        msg.To.Add(new MailAddress("xxxx@gmail.com"));
        msg.From = new MailAddress("xxxx@gmail.com");
        msg.Subject = "My app had an issue...";
        msg.Priority = MailPriority.High;

        //StringBuilder sb = new StringBuilder();
        //sb.Append(ctx.Request.Url.ToString() + System.Environment.NewLine);
        //sb.Append("Source:" + System.Environment.NewLine + ctx.Server.GetLastError().Source.ToString());
        //sb.Append("Message:" + System.Environment.NewLine + ctx.Server.GetLastError().Message.ToString());
        //sb.Append("Stack Trace:" + System.Environment.NewLine + ctx.Server.GetLastError().StackTrace.ToString());
        //msg.Body = sb.ToString();
        StringBuilder sb = new StringBuilder();
        sb.Append(ctx.Request.Url.ToString() + System.Environment.NewLine);
        sb.Append("Source:" + System.Environment.NewLine + err.Source.ToString());
        sb.Append("Message:" + System.Environment.NewLine + err.Message.ToString());
        sb.Append("ExceptionType:" + System.Environment.NewLine + err.GetType().ToString());
        sb.Append("TargetSite:" + System.Environment.NewLine + err.TargetSite.ToString());
        sb.Append("Timestamp:" + System.Environment.NewLine + DateTime.Now.ToString());
        msg.Body = sb.ToString();

        //CONFIGURE SMTP OBJECT
     
        SmtpClient smtp = new SmtpClient("smtp.gmail.com");
        smtp.EnableSsl = true;
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "xxxx");
        //SEND EMAIL
        smtp.Send(msg);

        //REDIRECT USER TO ERROR PAGE
        Server.Transfer("ErrorPage.aspx");
        //ExceptionManager.Publish(err);
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        Session["error"] = "";//initialize the session
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
     
</script>






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ErrorPage.aspx.cs" Inherits="ErrorPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <h2>
        An Error Has Occurred</h2>
    <p>
        An unexpected error occurred on our website. The website administrator has been notified.</p>
    <ul>
        <li>
            <asp:HyperLink ID="lnkHome" runat="server" NavigateUrl="~/Default.aspx">Return to the homepage</asp:HyperLink></li>
    </ul>
       <%-- <asp:Label ID="lblError" runat="server" Text="Label"></asp:Label>
         <asp:Label ID="lblExtendedMessage" runat="server" Text="Label"></asp:Label>--%>
    </div>
    </form>
</body>
</html>

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...