Introduction
In this article, I have shared how to log an exception in a text document so that it will be stored permanently in the database. I have attached the source code in the attachment section.
Write the following code in catch block of any event:
- protected void btnsave_Click(object sender, EventArgs e)
- {
- try
- {
-
- }
- catch(Exception ex)
- {
-
-
- lbldiserr.Text = "Something went wrong in Server, Kindly contact administator";
-
-
- string senderpagename = Path.GetFileNameWithoutExtension(Page.AppRelativeVirtualPath);
-
- LogException(ex, senderpagename);
-
- }
-
-
- }
-
- public static void LogException(Exception exc, string senderpagename)
- {
-
-
- string logFile = "~/App_Data/ErrorLog.txt";
-
- logFile = HttpContext.Current.Server.MapPath(logFile);
-
-
- StreamWriter sw = new StreamWriter(logFile, true);
- sw.WriteLine("********** {0} **********", DateTime.Now);
- if (exc.InnerException != null)
- {
- sw.Write("Inner Exception Type: ");
- sw.WriteLine(exc.InnerException.GetType().ToString());
- sw.Write("Inner Exception: ");
- sw.WriteLine(exc.InnerException.Message);
- sw.Write("Inner Source: ");
- sw.WriteLine(exc.InnerException.Source);
- if (exc.InnerException.StackTrace != null)
- {
- sw.WriteLine("Inner Stack Trace: ");
- sw.WriteLine(exc.InnerException.StackTrace);
- }
- }
- sw.Write("Exception ID:");
- sw.WriteLine(((System.Data.SqlClient.SqlException)(exc)).Number);
- sw.Write("Exception Type: ");
- sw.WriteLine(exc.GetType().ToString());
- sw.WriteLine("Exception: " + exc.Message);
- sw.WriteLine("Source: " + senderpagename);
- sw.WriteLine("Stack Trace: ");
- if (exc.StackTrace != null)
- {
- sw.WriteLine(exc.StackTrace);
- sw.WriteLine();
- }
-
-
- StackTrace stackTrace = new StackTrace();
- StackFrame stackFrame1 = stackTrace.GetFrame(1);
- MethodBase methodBase1 = stackFrame1.GetMethod();
-
-
- string Parent_Method_name = methodBase1.Name;
-
- SqlDatabase objdb = new SqlDatabase(OneStopMethods_Common.constring_Property);
- string exceptionno = ((System.Data.SqlClient.SqlException)(exc)).Number.ToString();
- string execeptiontype = exc.GetType().ToString();
- string message = exc.Message.Replace("'","");
- string sourcepage = senderpagename;
- string whereinsql = ((System.Data.SqlClient.SqlException)(exc)).Procedure;
-
- string orginalquery = " insert into adm_app_log(app_log_id,app_log_type,app_log_module_name,app_log_control_name,app_log_routine_name,app_log_desc,date_created,created_by,date_updated,updated_by) " +
- "values(Exceptionid,'Exception_Type','whichpageoccured','whichevent_itoccured','Sql_Detail','whatismessage',GETDATE(),'Admin',GETDATE(),'Admin')";
-
- string replacedquery = orginalquery.Replace("Exception_Type", execeptiontype).Replace("Exceptionid", exceptionno).Replace("whatismessage", message).Replace("whichevent_itoccured", Parent_Method_name).Replace("whichpageoccured", sourcepage).Replace("Sql_Detail", whereinsql);
-
-
-
- objdb.ExecuteDataSet(CommandType.Text, replacedquery);
-
-
-
-
-
-
- sw.Close();
- }
I hope the above information was helpful, kindly share your valuable feedback or thoughts.