Log 帮助类 队列
队列形式
1 using System; 2 using System.Collections; 3 using System.IO; 4 using System.Text; 5 using System.Threading; 6 using System.Windows.Threading; 7 8 namespace Helper 9 { 10 /// <summary> 11 /// Log 帮助类 12 /// </summary> 13 public static class LogHelper 14 { 15 /// <summary> 16 /// log 队列 17 /// </summary> 18 private static readonly Queue LogQueue = new Queue(); 19 20 /// <summary> 21 /// 判断 file stream 是否打开,用于单例 22 /// </summary> 23 private static bool _isStreamClose = true; 24 25 /// <summary> 26 /// 用于判断日志进程是否开启 27 /// </summary> 28 private static bool _isThreadBegin = false; 29 30 /// <summary> 31 /// 单例 文件写入方法 32 /// </summary> 33 private static StreamWriter _fileStreamWriter; 34 35 /// <summary> 36 /// 日志名称 37 /// </summary> 38 private static readonly string fileName =@"BugLog.txt"; 39 40 /// <summary> 41 /// 定时触发日志写入 42 /// </summary> 43 static int _intervalTime = 10000;// 一分钟 44 45 static System.Timers.Timer _timer = new System.Timers.Timer(_intervalTime); 46 47 /// <summary> 48 /// 添加日志队列 49 /// </summary> 50 /// <param name="message"></param> 51 public static void AddLog(string message) 52 { 53 string logContent = $"[{DateTime.Now:yyyy-MM-dd hh:mm:ss}] =>{message}"; 54 LogQueue.Enqueue(logContent); 55 if (!_isThreadBegin) 56 { 57 BeginThread(); 58 } 59 } 60 61 public static void AddLog(Exception ex) 62 { 63 var logContent = $"[{DateTime.Now:yyyy-MM-dd hh:mm:ss}]错误发生在:{ex.Source},\r\n 内容:{ex.Message}"; 64 logContent += $"\r\n 跟踪:{ex.StackTrace}"; 65 LogQueue.Enqueue(logContent); 66 if (!_isThreadBegin) 67 { 68 BeginThread(); 69 } 70 } 71 72 /// <summary> 73 /// 读取日志队列的一条数据 74 /// </summary> 75 /// <returns></returns> 76 private static object GetLog() 77 { 78 return LogQueue.Dequeue(); 79 } 80 81 /// <summary> 82 /// 开启定时查询线程 83 /// </summary> 84 public static void BeginThread() 85 { 86 _isThreadBegin = true; 87 88 //实例化Timer类,设置间隔时间为10000毫秒; 89 90 _timer.Interval = _intervalTime; 91 92 _timer.Elapsed += SetLog; 93 94 //到达时间的时候执行事件; 95 96 _timer.AutoReset = true; 97 98 //设置是执行一次(false)还是一直执行(true); 99 100 _timer.Enabled = true; 101 } 102 103 104 /// <summary> 105 /// 写入日志 106 /// </summary> 107 private static void SetLog(object source, System.Timers.ElapsedEventArgs e) 108 { 109 if (LogQueue.Count == 0) 110 { 111 if (_isStreamClose) return; 112 _fileStreamWriter.Flush(); 113 _fileStreamWriter.Close(); 114 _isStreamClose = true; 115 return; 116 } 117 if (_isStreamClose) 118 { 119 Isexist(); 120 string errLogFilePath = Environment.CurrentDirectory + @"\Log\" + fileName.Trim(); 121 if (!File.Exists(errLogFilePath)) 122 { 123 FileStream fs1 = new FileStream(errLogFilePath, FileMode.Create, FileAccess.Write); 124 _fileStreamWriter = new StreamWriter(fs1); 125 } 126 else 127 { 128 _fileStreamWriter = new StreamWriter(errLogFilePath, true); 129 } 130 _isStreamClose = false; 131 } 132 133 var strLog = new StringBuilder(); 134 135 var onceTime = 50; 136 137 var lineNum = LogQueue.Count > onceTime ? onceTime : LogQueue.Count; 138 139 for (var i = 0; i < lineNum; i++) 140 { 141 strLog.AppendLine(GetLog().ToString()); 142 } 143 144 _fileStreamWriter.WriteLine(strLog.ToString()); 145 146 } 147 148 /// <summary> 149 /// 判断是否存在日志文件 150 /// </summary> 151 private static void Isexist() 152 { 153 string path = Environment.CurrentDirectory + @"\Log\"; 154 if (!File.Exists(path)) 155 { 156 Directory.CreateDirectory(path); 157 } 158 } 159 } 160 }
/// <summary> /// 日记记录 /// </summary> public class LogHelper { //在网站根目录下创建日志目录(bin文件夹→debug文件夹→logs文件夹) public static string path = AppDomain.CurrentDomain.BaseDirectory + "Logs"; //死锁 public static object loglock = new object(); public static void Debug(string content) { WriteLog("DEBUG", content); } public static void Info(string content) { WriteLog("INFO", content); } public static void Test(string content) { WriteLog("Test", content); } public static void Error(string content) { WriteLog("ERROR", content); } protected static void WriteLog(string type, string content) { lock (loglock) { if (!Directory.Exists(path))//如果日志目录不存在就创建 { Directory.CreateDirectory(path); } string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//获取当前系统时间 string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名 //创建或打开日志文件,向日志文件末尾追加记录 StreamWriter mySw = File.AppendText(filename); //向日志文件写入内容 string write_content = time + " " + type + ": " + content+ Environment.NewLine; mySw.WriteLine(write_content); //关闭日志文件 mySw.Close(); } } }