/* * 日志记录 * 肖俊 * 2015-3-13 */ using NaiBa.Util; using System; using System.Collections.Generic; using System.IO; using System.Threading; using System.Windows.Forms; namespace AllBase { public class LogSave { /// /// 显示当前流程信息 key 1:拼团 2:拍单 3:售后 /// public static Dictionary> ShowMsg = new Dictionary>(); /// /// 消息日志 /// public static Dictionary> Messages = new Dictionary>(); /// /// 日志队列 /// private static ConcurrentList Logs = new ConcurrentList(); /// /// 准备写入文件 /// public static List ImageTemp = new List(); /// /// 初始化 /// public LogSave() { FileBase.DirectoryCreate(Application.StartupPath + "\\log\\"); FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\"); FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\province\\"); string[] l = Directory.GetFiles(Application.StartupPath + "\\log\\"); for (int i = 0; i < l.Length; i++) { string name = Path.GetFileName(l[i]); string date = BaseRegex.GetMatchRegexString(ref name, @"([\d]{4}-[\d]{1,2}-[\d]{1,2})"); DateTime d; if (DateTime.TryParse(date, out d)) { if (d < DateTime.Now.AddDays(-7)) { FileBase.Delete(l[i]); } } } } /// /// 服务 /// public void Get() { string temp; while (true) { if (Logs.Count == 0) { Thread.Sleep(10000); continue; } temp = ""; while (Logs.Count > 0) { try { if (string.IsNullOrEmpty(Logs[0])) continue; temp += Logs[0] + "\r\n"; Logs.RemoveAt(0); } catch { } } if (string.IsNullOrEmpty(temp)) continue; string patch = "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; try { File.AppendAllText(patch, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + temp); } catch (Exception e) { if (e.Message.Contains("空间")) return; Thread.Sleep(500);//重试一次 try { File.AppendAllText(patch, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + temp); } catch (Exception) { BaseClass.ViewCode += "写入IIS日志:" + patch + " 发生错误:" + e.Message + "\r\n"; } } Thread.Sleep(120 * 1000); } } /// /// 添加日志 /// /// 日志 public static void Add(string message) { BaseClass.ViewCode += message + "\r\n"; if (Logs.Count > 5000) { BaseClass.ViewCode += "日志队列超长,请检查日志处理\r\n"; Logs.Clear(); return; } Logs.Add(message); } /// /// 添加日志 /// /// 名称 /// 日志 public static void Add(string Name, string message) { if (string.IsNullOrEmpty(Name)) { Logs.Add(message); return; } if (!Messages.ContainsKey(Name)) { try { Messages.Add(Name, new List()); } catch { } } try { Logs.Add(message); Messages[Name].Add(message); } catch { } } /// /// 添加到显示 /// /// 日志 public static void AddView(string message) { BaseClass.ViewCode += message + "\r\n"; } /// /// 设置为最大值,完成 /// /// public static void SetProccessFinish(string name) { BaseClass.ProcessList[name].Value = BaseClass.ProcessList[name].Max; } public static void Save(string content, LogType logType) { try { if (logType == LogType.Operation) { string fileName = DateTime.Now.ToString("yyyy-MM-dd"); FileBase.WriteTxt("\\temp\\OperationLog", fileName, content, false); } if (logType == LogType.Error) { string fileName = DateTime.Now.ToString("yyyy-MM-dd"); FileBase.WriteTxt("\\temp\\ErrorLog", fileName, content, false); } } catch (Exception) { //throw; } } } public enum LogType { Operation, Error } }