The c implementation writes exceptions to the sample of exception log
- 2020-06-19 11:37:30
- OfStack
Writing an exception to a log file lets you debug a program and know which exceptions have occurred and where they occurred. This is especially useful for programs that need to run for a long time and be debugged.
/// <summary>
/// Print the exception to LOG file
/// </summary>
/// <param name="ex"> abnormal </param>
/// <param name="LogAddress"> Log file address </param>
public static void WriteLog(Exception ex, string LogAddress = "")
{
// If the log file is empty, the default is at Debug New directory YYYY-mm-dd_Log.log file
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
}
// Output the exception information to a file
StreamWriter fs = new StreamWriter(LogAddress, true);
fs.WriteLine(" Current time: " + DateTime.Now.ToString());
fs.WriteLine(" Exception information: " + ex.Message);
fs.WriteLine(" Exception object: " + ex.Source);
fs.WriteLine(" Call stack: \n" + ex.StackTrace.Trim());
fs.WriteLine(" Trigger method: " + ex.TargetSite);
fs.WriteLine();
fs.Close();
}