程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#寫文本日志幫助類(支持多線程)改進版(不適用於ASP.NET程序),

C#寫文本日志幫助類(支持多線程)改進版(不適用於ASP.NET程序),

編輯:C#入門知識

C#寫文本日志幫助類(支持多線程)改進版(不適用於ASP.NET程序),


由於iis的自動回收機制,不適用於ASP.NET程序

代碼:

using System; using System.Collections.Concurrent; using System.Configuration; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CommonDll { /// <summary> /// 寫日志類 /// </summary> public class LogUtil { #region 字段 public static string path = ConfigurationManager.AppSettings["LogPath"]; public static int fileSize = 10 * 1024 * 1024; //日志分隔文件大小 private static ConcurrentQueue<Tuple<string, DateTime>> queue = new ConcurrentQueue<Tuple<string, DateTime>>(); #endregion #region 構造函數 static LogUtil() { Task.Factory.StartNew(new Action(delegate() { StringBuilder log; string path; Tuple<string, DateTime> tuple; string item; while (true) { log = new StringBuilder(); path = CreateLogPath(); while (queue.TryDequeue(out tuple)) { item = string.Format(@"{0} {1}", tuple.Item2.ToString("yyyy-MM-dd HH:mm:ss.fff"), tuple.Item1); log.AppendFormat("\r\n{0}", item); } if (log.Length > 0) WriteFile(log.ToString(2, log.Length - 2), path); Thread.Sleep(100); } })); } #endregion #region 寫文件 /// <summary> /// 寫文件 /// </summary> public static void WriteFile(string log, string path) { try { if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } if (!File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Close(); } } using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(log); sw.Flush(); } fs.Close(); } } catch { } } #endregion #region 生成日志文件路徑 /// <summary> /// 生成日志文件路徑 /// </summary> public static string CreateLogPath() { int index = 0; string logPath; bool bl = true; do { index++; logPath = Path.Combine(path, "Log" + DateTime.Now.ToString("yyyyMMdd") + (index == 1 ? "" : "_" + index.ToString()) + ".txt"); if (File.Exists(logPath)) { FileInfo fileInfo = new FileInfo(logPath); if (fileInfo.Length < fileSize) { bl = false; } } else { bl = false; } } while (bl); return logPath; } #endregion #region 寫錯誤日志 /// <summary> /// 寫錯誤日志 /// </summary> public static void LogError(string log) { queue.Enqueue(new Tuple<string, DateTime>("[Error] " + log, DateTime.Now)); } #endregion #region 寫操作日志 /// <summary> /// 寫操作日志 /// </summary> public static void Log(string log) { queue.Enqueue(new Tuple<string, DateTime>("[Info] " + log, DateTime.Now)); } #endregion } } View Code

測試代碼:

private void button1_Click(object sender, EventArgs e) { int n = 10000; DateTime dtStart = DateTime.Now; for (int i = 1; i <= n; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj) { int j = (int)obj; LogUtil.Log("測試" + j.ToString("00000")); if (j == n) { double sec = DateTime.Now.Subtract(dtStart).TotalSeconds; MessageBox.Show(n + "條日志完成,耗時" + sec.ToString("0.000") + "秒"); } }), i); } } View Code

效果圖:

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved