using System; using System.Collections.Generic; using System.Text; using System.IO; namespace ProvinceUpdateExe.Lib { /// /// 日志类 /// public class ClassLog { //日志文件所在路径 //private static string logPath = string.Empty; private static string logPath = AppDomain.CurrentDomain.BaseDirectory; /// /// 保存日志的文件夹 /// public static string LogPath { get { if (logPath == string.Empty) { logPath = AppDomain.CurrentDomain.BaseDirectory; } return logPath; } set { logPath = value; } } //日志前缀说明信息 //private static string logFielPrefix = string.Empty; private static string logFielPrefix = "errorlog"; /// /// 日志文件前缀 /// public static string LogFielPrefix { get { return logFielPrefix; } set { logFielPrefix = value; } } /// /// 写日志 /// 日志类型 /// 日志内容 /// public static void WriteLog(string logType, string msg) { System.IO.StreamWriter sw = null; try { FileStream _FileStream; if (!File.Exists(LogPath + logFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".log")) { _FileStream = new FileStream(LogPath + logFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.CreateNew); sw = new StreamWriter(_FileStream); _FileStream.Close(); } //同一天同一类日志以追加形式保存 sw = File.AppendText(LogPath + logFielPrefix + DateTime.Now.ToString("yyyyMMdd") + ".log"); sw.WriteLine(logType + "\t#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss \t") + msg); } catch { } finally { sw.Close(); } } /// /// 写日志 /// public void WriteLog(LogType logType, string msg) { WriteLog(logType.ToString(), msg); } /// /// 日志类型 /// public enum LogType { /// /// 调试信息 /// Debug, /// /// 日常信息 /// Info, /// /// 警告信息 /// Warning, /// /// 错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息 /// Error, /// /// 与数据库相关的信息 /// SQL } } }