public static class LogHelper { //日志存储路径 private static string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.Configuration.ConfigurationManager.AppSettings["LogPath"]); private static object LogLock = new object();//日志锁 /// <summary> /// 添加正常信息 /// </summary> /// <param name="message"></param> public static void AddInfo(string message) { string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称 string fullName = Path.Combine(LogPath, fileName); lock (LogLock) { if (!Directory.Exists(LogPath))//如果目录不存在 创建目录 { Directory.CreateDirectory(LogPath); } using (var stream = File.AppendText(fullName)) { stream.WriteLine(message); } Console.WriteLine(message); } } /// <summary> /// 添加错误信息 /// </summary> /// <param name="errorMessage"></param> public static void AddError(string errorMessage) { string fileName ="Error_"+ DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称 string fullName = Path.Combine(LogPath, fileName); lock (LogLock) { if (!Directory.Exists(LogPath))//如果目录不存在 创建目录 { Directory.CreateDirectory(LogPath); } using (var stream = File.AppendText(fullName)) { stream.WriteLine(errorMessage); } Console.WriteLine(errorMessage); } } }
时间: 2024-11-06 13:37:21