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
- {
- //code requires as per your requirement
- }
- catch(Exception ex)
- {
- //show some appropriate message to the user
- lbldiserr.Text = "Something went wrong in Server, Kindly contact administator";
- //getting page name also -it is optional
- string senderpagename = Path.GetFileNameWithoutExtension(Page.AppRelativeVirtualPath);
- LogException(ex, senderpagename);
- }
- }
- public static void LogException(Exception exc, string senderpagename)
- {
- // Include enterprise logic for logging exceptions
- // Get the absolute path to the log file
- string logFile = "~/App_Data/ErrorLog.txt";
- logFile = HttpContext.Current.Server.MapPath(logFile);
- // Open the log file for append and write the log
- 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();
- // Displays “WhatsmyName”
- 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);
- //if (EventLog.SourceExists("karthik_Testing"))
- //{
- // EventLog eventlog = new EventLog("TPMS_Log");
- // eventlog.Source = "TPMS_Log";
- // eventlog.WriteEntry(sw.ToString(), EventLogEntryType.Error);
- //}
- sw.Close();
- }

Karthik ElumalaiPosted Jul 6, 2016, 1:20 AM
Thanks a lot for your positive feedback dude..It's really motivates me and will surely work hard for my dream MVP award.. But now itself am really happy that, by knowing ,some of my articles really helping others a little bit. Once again thanks for your precious time ,I really appreciate it and my hearty wishes for your growth too..Love sharing alaways ..:) @Subash
SubashPosted Jul 6, 2016, 12:47 AM
Advance wishes to get MVP award
SubashPosted Jul 6, 2016, 12:47 AM
No Mention bro i some of your articles all i read was very nice but something difficult to which concepts are new for me
Karthik ElumalaiPosted Jul 5, 2016, 11:41 AM
My pleasure..:)And thanks for precious time and feedbackSubash
SubashPosted Jul 4, 2016, 5:01 AM
Thanks