码迷,mamicode.com
首页 > 编程语言 > 详细

分享一个记录日志的类,可多线程使用。

时间:2014-12-05 22:40:17      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

好久没写博客了,今天分享一个自己用的日志类,非原创,借鉴了前辈的一个想法,然后修改来的。

日志我们是必须的,现在程序都是多线程并发了,记日志就有可能出现问题了,lock?影响性能。log4net太重量级了,本日志是一个轻量级的小工具。

 废话不多说,看源码:

 

bubuko.com,布布扣
  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);

 

如有不足还请指教。

分享一个记录日志的类,可多线程使用。

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/moretry/p/4147559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!