Monday, February 11, 2013

code for catching error and saving in database in asp.net


 void Application_Error(object sender, EventArgs e)
    {
        //Code that runs when an unhandled error occurs
     
        //PS: This functionality stores all errors in database and these errors can be vieved
        //directly in database in 'ErrorLog' table. Or can also be viewed by browsing the
        //page 'errorlog.aspx' directly from the root of application.
     
        //Note: This logic will failed only in the case;
        //if database connection will not established due to some reasons.
        //so this is single case and can be trapped by developers. OR
        //In web.config; set <customErrors mode="Off"> and see the error.

        clsGenericClass objGen = new clsGenericClass();

        Exception objErr = new Exception();
        objErr = Server.GetLastError().GetBaseException();

        objGen.InsertErrorLog(objErr.Message, "Error Caught in Application_Error event",
            Request.Url.ToString(), objErr.Source, "", DateTime.Now);

        Server.ClearError();
    }

No comments:

Post a Comment