1 /// <summary> 2 /// 写入日志到文本文件 3 /// </summary> 4 /// <param name="userName">用户名称</param> 5 /// <param name="action">动作</param> 6 /// <param name="content">日志内容</param> 7 /// <param name="result">操作结果或报错信息</param> 8 /// <param name="msg">报错信息</param> 9 public static void WriteTextLog(string userName, string action, string content, string result, string msg) 10 { 11 try 12 { 13 DateTime time = DateTime.Now; 14 string path = AppDomain.CurrentDomain.BaseDirectory + "/Log/" + time.ToString("yyyy-MM") + "/"; 15 if (!Directory.Exists(path)) 16 Directory.CreateDirectory(path); 17 18 string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".txt"; 19 StringBuilder str = new StringBuilder(); 20 str.Append("用户: " + userName + " "); 21 str.Append("时间: " + time.ToString() + " "); 22 str.Append("类型: " + action + " "); 23 str.Append("内容: " + content + " "); 24 str.Append("结果: " + result + " "); 25 str.Append("备注: " + msg + " "); 26 str.Append("----------------------------------------------------------- "); 27 StreamWriter sw; 28 if (!File.Exists(fileFullPath)) 29 { 30 sw = File.CreateText(fileFullPath); 31 } 32 else 33 { 34 sw = File.AppendText(fileFullPath); 35 } 36 sw.WriteLine(str.ToString()); 37 sw.Close(); 38 } 39 catch (Exception) 40 { 41 42 } 43}