国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-17 02:43:20
字體:
供稿:網(wǎng)友

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

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

日志我們是必須的,現(xiàn)在程序都是多線程并發(fā)了,記日志就有可能出現(xiàn)問題了,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         /// 獲取或設(shè)置文件名稱   18         /// </summary>   19         public string FileName 20         { 21             get { return _fileName; } 22             set { _fileName = value; } 23         } 24  25         /// <summary>   26         /// 構(gòu)造函數(shù)   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         /// 創(chuàng)建文件路徑 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">文本內(nèi)容</param> 60         /// <param name="newLine">換行標(biāo)記</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         /// 寫入文件內(nèi)容  100         /// </summary>  101         /// <param name="content">內(nèi)容</param>  102         public void WriteLine(string content)103         {104             this.Write(content, Environment.NewLine);105         }106 107         /// <summary>  108         /// 寫入文件內(nèi)容  不換行109         /// </summary>  110         /// <param name="content">內(nèi)容</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);

如有不足還請指教。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新沂市| 襄垣县| 本溪市| 肇东市| 邯郸市| 崇信县| 大理市| 广宁县| 四平市| 定南县| 渝中区| 诸城市| 洪江市| 什邡市| 株洲市| 凌云县| 池州市| 砚山县| 肥西县| 始兴县| 迁安市| 东台市| 兴海县| 高淳县| 滕州市| 巧家县| 巴东县| 大理市| 保定市| 翁牛特旗| 民县| 神农架林区| 仁布县| 喀喇沁旗| 阿瓦提县| 天等县| 密山市| 天门市| 云浮市| 建德市| 广宗县|