using System.IO; public class PublicWriteError { private static StreamWriter streamWriter; /// <summary> /// /// </summary> /// <param name="message">报错信息</param> /// <param name="type"></param> /// <param name="FFname">方法名称</param> public static void WriteError(string message, string type, string FFname) { try { //DateTime dt = new DateTime(); string directPath = "D://DJErrorLog"; //在获得文件夹路径 //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log"; if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建 { Directory.CreateDirectory(directPath); } directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd")); if (streamWriter == null) { streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); //判断文件是否存在如果不存在则创建,如果存在则添加。 } streamWriter.WriteLine("***********************************************************************"); streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); streamWriter.WriteLine("输出信息:" + type + ""); streamWriter.WriteLine("方法名称:" + FFname + ""); if (message != null) { streamWriter.WriteLine("信息: " + message); } } finally { if (streamWriter != null) { streamWriter.Flush(); streamWriter.Dispose(); streamWriter = null; } } } /// <summary> /// /// </summary> /// <param name="ex"></param> /// <param name="Message">报错信息</param> /// <param name="FFname">方法名称</param> public static void WriteError(Exception ex, string Message, string FFname) { try { //DateTime dt = new DateTime(); string directPath = "D://DJErrorLog"; //在获得文件夹路径 //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log"; if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建 { Directory.CreateDirectory(directPath); } directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd")); if (streamWriter == null) { streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); //判断文件是否存在如果不存在则创建,如果存在则添加。 } streamWriter.WriteLine("***********************************************************************"); streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); streamWriter.WriteLine("输出信息:" + Message + ""); streamWriter.WriteLine("方法名称:" + FFname + ""); streamWriter.WriteLine("错误信息: " + ex.Message); streamWriter.WriteLine("栈堆信息: " + ex.StackTrace); } finally { if (streamWriter != null) { streamWriter.Flush(); streamWriter.Dispose(); streamWriter = null; } } } /// <summary> /// /// </summary> /// <param name="ex"></param> /// <param name="FFname">方法名称</param> public static void WriteError(Exception ex, string FFname) { try { //DateTime dt = new DateTime(); string directPath = "D://DJErrorLog"; //在获得文件夹路径 //string directPath = AppDomain.CurrentDomain.BaseDirectory+"/log"; if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建 { Directory.CreateDirectory(directPath); } directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd")); if (streamWriter == null) { streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); //判断文件是否存在如果不存在则创建,如果存在则添加。 } streamWriter.WriteLine("***********************************************************************"); streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); streamWriter.WriteLine("输出信息:错误信息"); streamWriter.WriteLine("方法名称:" + FFname + ""); streamWriter.WriteLine("错误信息: " + ex.Message); streamWriter.WriteLine("栈堆信息: " + ex.StackTrace); } finally { if (streamWriter != null) { streamWriter.Flush(); streamWriter.Dispose(); streamWriter = null; } } } }