今天分享一个自己用的日志类,非原创,借鉴了前辈的一个想法,然后修改来的。
日志我们是必须的,现在程序都是多线程并发了,记日志就有可能出现问题了,lock?影响性能。log4net太重量级了,本日志是一个轻量级的小工具。
废话不多说,看源码:
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace GEDU.CourseOnline.Common { /// <summary> /// 日志类(多线程版) /// </summary> public class LogHelper { private string _fileName; private static Dictionary<long, long> lockDic = new Dictionary<long, long>(); /// <summary> /// 获取或设置文件名称 /// </summary> public string FileName { get { return _fileName; } set { _fileName = value; } } /// <summary> /// 构造函数 /// </summary> /// <param name="fileName">文件全路径名</param> public LogHelper(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new Exception("FileName不能为空!"); } Create(fileName); _fileName = fileName; } /// <summary> /// 创建文件路径 /// </summary> /// <param name="fileName">文件路径</param> public void Create(string fileName) { var directoryPath = Path.GetDirectoryName(fileName); if (string.IsNullOrEmpty(directoryPath)) { throw new Exception("FileName路径错误!"); } if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } } /// <summary> /// 写入文本 /// </summary> /// <param name="content">文本内容</param> /// <param name="newLine">换行标记</param> private void Write(string content, string newLine) { using (FileStream fs = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.Asynchronous)) { Byte[] dataArray = Encoding.UTF8.GetBytes(content + newLine); bool flag = true; long slen = dataArray.Length; long len = 0; while (flag) { try { if (len >= fs.Length) { fs.Lock(len, slen); lockDic[len] = slen; flag = false; } else { len = fs.Length; } } catch (Exception ex) { while (!lockDic.ContainsKey(len)) { len += lockDic[len]; } } } fs.Seek(len, SeekOrigin.Begin); fs.Write(dataArray, 0, dataArray.Length); fs.Close(); } } /// <summary> /// 写入文件内容 /// </summary> /// <param name="content">内容</param> public void WriteLine(string content) { this.Write(content, Environment.NewLine); } /// <summary> /// 写入文件内容 不换行 /// </summary> /// <param name="content">内容</param> public void Write(string content) { this.Write(content, ""); } } }
用法:
string strPath = HttpContext.Current.Server.MapPath(string.Format("/Log/{0:yyyyMMdd}.txt", DateTime.Today)); //string strPath = string.Format(@"D:\Log\{0:yyyyMMdd}.txt", DateTime.Today); LogHelper logHelper = new LogHelper(strPath); logHelper.WriteLine(sWord + " 时间:" + DateTime.Now);
评论回复