zoukankan      html  css  js  c++  java
  • 分享一个记录日志的类,可多线程使用。

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

    日志我们是必须的,现在程序都是多线程并发了,记日志就有可能出现问题了,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);

    如有不足还请指教。

  • 相关阅读:
    河北省科技创新年报统计系统分析
    《软件需求十步走》阅读笔记06
    《软件需求十步走》阅读笔记05
    《软件需求十步走》阅读笔记04
    河北科技创新平台年报统计
    《软件需求十步走》阅读笔记03
    《软件需求十步走》阅读笔记02
    《软件需求十步走》阅读笔记01
    案例分析
    2017秋季个人阅读计划
  • 原文地址:https://www.cnblogs.com/moretry/p/4147559.html
Copyright © 2011-2022 走看看