Search This Blog

Tuesday, November 16, 2010

Code Generate the Error log file

Hello all..
Here i am putting the code to generate the error in log file.I think it is much helpful for those who want to generate error in a folder which is not shown to the user.
Put the following Code in your project.

At Default.apsx Page



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


<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="FirstNumber"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="SecondNumber"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="submit"
            Width="135px" />
        <br />
   
    </div>
    </form>
</body>
</html>




 Default.cs Page



using System;

using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    int FirstNumber;
    int SecondNumber;
    int result;
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            FirstNumber = Convert.ToInt32(TextBox1.Text);
            SecondNumber = Convert.ToInt32(TextBox2.Text);
            result = FirstNumber / SecondNumber;
            Response.Write("result is:" + result.ToString());
        }
        catch (Exception ex)
        {

            throw;
        }
        finally
        {
       
        }
    }
}


Add new folder in your application and named it As MyError.in this folder the error is generate in a text file

Now Go into Solution Explorer and add new item as Global.asax page and put the following code in Application_Error Event.



Global.asax  page



//write the code at Application_error event
Exception err = (Exception)Server.GetLastError().InnerException;

        //Create a text file containing error details

        string strFileName = "Err_dt_" + DateTime.Now.Month + "_" +      DateTime.Now.Day
        + "_" + DateTime.Now.Year + "_Time_" + DateTime.Now.Hour + "_" +
        DateTime.Now.Minute + "_" + DateTime.Now.Second + "_"
        + DateTime.Now.Millisecond + ".txt";

        strFileName = Server.MapPath("~") + "\\MyError\\" + strFileName;

        FileStream fsOut = File.Create(strFileName);
        StreamWriter sw = new StreamWriter(fsOut);

        //Log the error details

        string errorText = "Error Message: " + err.Message + sw.NewLine;
        errorText = errorText + "Stack Trace: " + err.StackTrace + sw.NewLine;
        sw.WriteLine(errorText);
        sw.Flush();
        sw.Close();
        fsOut.Close();
        Response.Redirect("erroor.aspx")






Now add new .aspx page as error.axpx.we use this page to show some other error which is not acctual error.


error.aspx page



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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    sorry !!!!!!!!!!
    You are attempting some wrong.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Back</asp:LinkButton>
   
   
    </div>
    </form>
</body>
</html>




Also we have to redirect the user to main page.So we take the error.cs page as


error.cs Page


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class erroor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}




I think it is much useful for you.
Please comment if you have any Query,Suggestion or you like it.

Thanks






4 comments :

RAJ said...

nice code its working fine in my end.
Thanks good job for tracking error as a log file.

Chandra Prakash Yadav said...

Thanks Ashish for sharing your code.

DIRECT LINK said...

thanks this is really helpfull

Anonymous said...

hi