Log输出
1 using System.IO; 2 using System; 3 public class OutputLog 4 { 5 #region 添加Log 6 public static string logFilePath = System.Configuration.ConfigurationSettings.AppSettings["LogFile"].ToString(); 7 private static void AppendLogs(string logMessage) 8 { 9 try 10 { 11 if (!Directory.Exists(logFilePath)) 12 { 13 Directory.CreateDirectory(logFilePath); 14 } 15 16 string path = logFilePath + "Log.txt"; 17 StreamWriter sw = null; 18 if (!File.Exists(path)) 19 { 20 sw = File.CreateText(path); 21 } 22 else 23 { 24 sw = File.AppendText(path); 25 } 26 sw.WriteLine(); 27 sw.WriteLine("时间:" + DateTime.Now); 28 sw.WriteLine(logMessage); 29 sw.Flush(); 30 sw.Close(); 31 } 32 catch (System.Exception ex) 33 { 34 throw ex; 35 } 36 } 37 #endregion 38 }
分享每天的收获之一种快乐。