/* * 日志记录 * 肖俊 * 2015-3-13 */ using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using System.Windows.Forms; namespace AllBase { public class LogSave { /// /// 消息日志 /// public static Dictionary> Messages = new Dictionary>(); /// /// 日志队列 /// private static List Logs = new List(); /// /// 准备写入文件 /// public static List ImageTemp = new List(); /// /// 初始化 /// public LogSave() { FileBase.DirectoryCreate(Application.StartupPath + "\\log\\"); FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\"); FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\content\\"); FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\desc\\"); 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 /// 服务 /// public void Get() { string temp; while (true) { if (Logs.Count == 0 && ImageTemp.Count==0) { Thread.Sleep(10000); continue; } StringBuilder s = new StringBuilder(); while (ImageTemp.Count>0) { string it = ImageTemp[0]; ImageTemp.RemoveAt(0); if (string.IsNullOrEmpty(it)) continue; s.AppendLine(it); } string image = s.ToString(); if (image.Length > 0) FileBase.AppendAllText("image/" + BaseClass.ShopName+".tjm", image, Encoding.Default); temp = ""; while (Logs.Count>0) { try { 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 SetProccessMax(string name, int max) { if (!BaseClass.ProcessList.ContainsKey(name)) { try { BaseClass.ProcessList.Add(name,new ProcessBase()); } catch { } } BaseClass.ProcessList[name].Max = max; } /// /// 设置当前值 /// /// 名称 /// 当前值 public static void SetProccessValue(string name, int value) { if (!BaseClass.ProcessList.ContainsKey(name)) { try { BaseClass.ProcessList.Add(name, new ProcessBase()); } catch { } } BaseClass.ProcessList[name].Value = value; } /// /// 设置为最大值,完成 /// /// public static void SetProccessFinish(string name) { BaseClass.ProcessList[name].Value = BaseClass.ProcessList[name].Max; } } }