程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 分享一個記錄日志的類,可多線程使用。,日志多線程

分享一個記錄日志的類,可多線程使用。,日志多線程

編輯:C#入門知識

分享一個記錄日志的類,可多線程使用。,日志多線程


好久沒寫博客了,今天分享一個自己用的日志類,非原創,借鑒了前輩的一個想法,然後修改來的。

日志我們是必須的,現在程序都是多線程並發了,記日志就有可能出現問題了,lock?影響性能。log4net太重量級了,本日志是一個輕量級的小工具。

 廢話不多說,看源碼:

 

1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Text; 5 6 namespace GEDU.CourseOnline.Common 7 { 8 /// <summary> 9 /// 日志類(多線程版) 10 /// </summary> 11 public class LogHelper 12 { 13 private string _fileName; 14 private static Dictionary<long, long> lockDic = new Dictionary<long, long>(); 15 16 /// <summary> 17 /// 獲取或設置文件名稱 18 /// </summary> 19 public string FileName 20 { 21 get { return _fileName; } 22 set { _fileName = value; } 23 } 24 25 /// <summary> 26 /// 構造函數 27 /// </summary> 28 /// <param name="fileName">文件全路徑名</param> 29 public LogHelper(string fileName) 30 { 31 if (string.IsNullOrEmpty(fileName)) 32 { 33 throw new Exception("FileName不能為空!"); 34 } 35 Create(fileName); 36 _fileName = fileName; 37 } 38 39 /// <summary> 40 /// 創建文件路徑 41 /// </summary> 42 /// <param name="fileName">文件路徑</param> 43 public void Create(string fileName) 44 { 45 var directoryPath = Path.GetDirectoryName(fileName); 46 if (string.IsNullOrEmpty(directoryPath)) 47 { 48 throw new Exception("FileName路徑錯誤!"); 49 } 50 if (!Directory.Exists(directoryPath)) 51 { 52 Directory.CreateDirectory(directoryPath); 53 } 54 } 55 56 /// <summary> 57 /// 寫入文本 58 /// </summary> 59 /// <param name="content">文本內容</param> 60 /// <param name="newLine">換行標記</param> 61 private void Write(string content, string newLine) 62 { 63 using (FileStream fs = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.Asynchronous)) 64 { 65 Byte[] dataArray = Encoding.UTF8.GetBytes(content + newLine); 66 bool flag = true; 67 long slen = dataArray.Length; 68 long len = 0; 69 while (flag) 70 { 71 try 72 { 73 if (len >= fs.Length) 74 { 75 fs.Lock(len, slen); 76 lockDic[len] = slen; 77 flag = false; 78 } 79 else 80 { 81 len = fs.Length; 82 } 83 } 84 catch (Exception ex) 85 { 86 while (!lockDic.ContainsKey(len)) 87 { 88 len += lockDic[len]; 89 } 90 } 91 } 92 fs.Seek(len, SeekOrigin.Begin); 93 fs.Write(dataArray, 0, dataArray.Length); 94 fs.Close(); 95 } 96 } 97 98 /// <summary> 99 /// 寫入文件內容 100 /// </summary> 101 /// <param name="content">內容</param> 102 public void WriteLine(string content) 103 { 104 this.Write(content, Environment.NewLine); 105 } 106 107 /// <summary> 108 /// 寫入文件內容 不換行 109 /// </summary> 110 /// <param name="content">內容</param> 111 public void Write(string content) 112 { 113 this.Write(content, ""); 114 } 115 } 116 } View Code

 

用法:

1 string strPath = HttpContext.Current.Server.MapPath(string.Format("/Log/{0:yyyyMMdd}.txt", DateTime.Today));
2 //string strPath = string.Format(@"D:\Log\{0:yyyyMMdd}.txt", DateTime.Today);
3 LogHelper logHelper = new LogHelper(strPath);
4 logHelper.WriteLine(sWord + "  時間:" + DateTime.Now);

 

如有不足還請指教。

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