using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace HZQR.Common { /// /// 文本日志帮助类 /// public class LogUtil { /// /// 记录文本日志 /// /// 异常 public static void WriteLog(Exception ex) { WriteLog(ex, ""); } /// /// 记录文本日志 /// /// 日志内容 public static void WriteLog(string msg) { WriteLog(null, msg); } /// /// 记录文本日志 /// /// 异常 /// 日志内容 public static void WriteLog(Exception ex, string otherMsg) { WriteLog(ex, otherMsg, ""); } /// /// 记录文本日志 /// /// /// /// public static void WriteLog(Exception ex, string otherMsg, string FileName) { WriteLog(ex, otherMsg, FileName, ""); } /// /// 记录文本日志 /// /// 异常 /// 日志内容 /// 日志文件名称 /// 文件存储路径 public static void WriteLog(Exception ex, string otherMsg, string FileName, string DirectoryName) { string str = ""; try { if (string.IsNullOrEmpty(FileName)) { FileName = DateTime.Now.ToString("yyyyMMdd"); } if (string.IsNullOrWhiteSpace(DirectoryName)) { DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "log"; } str += string.Format(@"{0}", DateTime.Now.ToString()); if (ex != null) { str += string.Format(@" Message:{0} StackTrace: {1} Source:{2} " , ex.Message , ex.StackTrace , ex.Source ); } if (!string.IsNullOrWhiteSpace(otherMsg)) { str += string.Format("{0}", otherMsg); } if (!Directory.Exists(DirectoryName))//判断文件夹是否存在 { Directory.CreateDirectory(DirectoryName);//不存在则创建文件夹 } string filePath = DirectoryName + "\\" + FileName + ".txt"; StreamWriter sw = new StreamWriter(filePath, true); sw.WriteLine(str); sw.Close(); } catch (Exception exception) { WriteLog(exception, "", "", @"D:\RebuildLog"); } } } }