Tuesday, August 13, 2019

Try and Catch , Logger Message with Showing Line Number C#

 

try{
}
catch(Error iwnien)
 StackTrace st = new StackTrace(iwnien, true);
 StackFrame[] frames = st.GetFrames();

 var path = System.IO.Path.Combine(Application.StartupPath, "Log.txt");


 // Iterate over the frames extracting the information you need


 StringBuilder error_string = new StringBuilder();
 foreach (StackFrame frame in frames)
 {
 try {

  error_string.AppendLine( frame.GetMethod().Name.ToString() + frame.GetFileLineNumber() );
 } catch { }

 //Console.WriteLine("{0}:{1}({2},{3})", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber());
 }

 System.IO.File.AppendAllLines(path, new string[] { error_string.ToString() });

}