一个简单的日志类,测试时候用 Code 1 class LogWriter 2 { 3 private static string path = Application.StartupPath + "\\log.txt"; 4 static LogWriter() 5 { 6 7 if (!File.Exists(path)) 8 { 9 FileStream fs = File.Create(path); 10 fs.Close(); 11 } 12 13 Log("Log Start:"); 14 } 15 16 public static void Log(string str) 17 { 18 StreamWriter sw = File.AppendText(path); 19 sw.WriteLine(DateTime.Now.ToLongTimeString() + ": " + str); 20 sw.Close(); 21 } 22 23 public static void Error(string str) 24 { 25 StreamWriter sw = File.AppendText(path); 26 sw.WriteLine(DateTime.Now.ToLongTimeString() + ": Error " + str); 27 sw.Close(); 28 } 29 }