程序的错误日志如何记录下来?
可以在遇到异常时,Catch异常,然后把异常的信息输出到txt文件中即可
/// <summary> /// 错误日志 /// </summary> public class LogHelper { private static object lockFlag = new object(); private static string LOG_FILE_PATH = AppDomain.CurrentDomain.BaseDirectory + "Log"; /// <summary> /// 记录日志 /// 异常 /// </summary> /// <param name="ex"></param> public static void LogError(Exception ex) { LogInfo(String.Format("ExType:{0} ExMessage:{1} ExSource:{2} ExStack: {3} ", ex.GetType(), ex.Message, ex.Source, ex.StackTrace)); } public static void LogInfo(string msg) { lock (lockFlag) { IOHelper.CreateDirectory(LOG_FILE_PATH); string fileName = LOG_FILE_PATH + "\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; msg = String.Format("------------------------{0}------------------------------ ", DateTime.Now) + msg; msg += " "; File.AppendAllText(fileName, msg); } } }