随便写写之日志类
1 using System; 2 using System.IO; 3 using System.Text; 4 5 namespace CaptureData 6 { 7 /// <summary> 8 /// 随便写的一个日志类哦 9 /// </summary> 10 public class Log 11 { 12 private static object obj = new object(); 13 14 public static void LogInfo(Object info) 15 { 16 var now = DateTime.Now; 17 StringBuilder str = new StringBuilder(); 18 str.Append(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); 19 str.Append("\r\n"); 20 str.Append(info); 21 str.Append("\r\n"); 22 write(str.ToString()); 23 } 24 25 public static void LogInfoWithSession(string si, Object info) 26 { 27 var now = DateTime.Now; 28 StringBuilder str = new StringBuilder(); 29 str.Append(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); 30 str.Append("----" + si); 31 str.Append("\r\n"); 32 str.Append(info); 33 str.Append("\r\n\r\n"); 34 write(str.ToString()); 35 } 36 public static void write(string info) 37 { 38 var now = DateTime.Now; 39 var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log"); 40 if (!Directory.Exists(path)) 41 Directory.CreateDirectory(path); 42 path = Path.Combine(path, now.ToString("yyyyMMddHH") + ".log"); 43 lock (typeof(Log)) 44 //lock (obj) 45 { 46 File.AppendAllText(path, info); 47 } 48 } 49 } 50 }