非常好用的异步日志记录类
来源:互联网 发布:python scapy 编辑:程序博客网 时间:2024/06/10 01:59
日常工作需要,结合前辈的经验,写了一个通用的日志记录类,感觉比较完善了,没有用到队列,只是简单的异步安全写入。
vs2010
using System;using System.Text;using System.IO;using System.Configuration;using System.Threading;using System.Reflection;using System.Diagnostics;namespace Wyw.Utilities{ /// <summary> /// LogHelper /// 异步日志类 /// /// 修改记录 ///20120312 全部采用了静态,增加了取方法位置函数与每天新日志 ///20120404 增加了消息体结构及方法 /// 版本:1.0 /// <author> ///<name>Wyw308</name> ///<date>2012.03.11</date> /// </author> /// </summary> public class LogHelper { private static string filePath = "log\\"; private static string fileName = DateTime.Now.ToString("yyyyMMdd") + ".log"; /// <summary> /// 文件路径 /// </summary> public static string FilePath { get { return filePath; } set { filePath = value; } } /// <summary> /// 文件名 /// </summary> public static string FileName { get { return fileName; } set { fileName = value; } } static Mutex myMutex = new Mutex(); private delegate void WriteStrToFileDelegate(Msg msg); #region 将消息体写入日志文件 /// <summary> /// 将消息体写入日志文件 /// </summary> /// <param name="msg">消息体</param> private static void WriteMsgToFile(Msg msg) { if (!Directory.Exists(LogHelper.FilePath)) Directory.CreateDirectory(LogHelper.FilePath); StreamWriter sw = null; myMutex.WaitOne(); //线程互斥 FileInfo finfo = new FileInfo(filePath + "\\" + fileName); if (!finfo.Exists) { sw = File.CreateText(filePath + "\\" + fileName); } else { sw = new StreamWriter(finfo.OpenWrite()); } sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine(string.Format("{0}", msg.Datetime) + "\t" + msg.Type + "\t" + msg.Location + "\t" + msg.Text); sw.Flush(); sw.Close(); myMutex.ReleaseMutex(); } #endregion #region 外部调用 /// <summary> /// 获取方法类名等位置 /// </summary> /// <param name="frameIndex">方法级别,默认1为调用它的方法名</param> /// <returns>返回这样形式:shenghua_riba.Form1.WriMsg()</returns> public static string GetMethodLocal(int frameIndex) { string str = string.Empty; StackTrace ss = new StackTrace(true); if (ss.FrameCount < frameIndex) frameIndex = ss.FrameCount; MethodBase mb = ss.GetFrame(frameIndex).GetMethod(); ////取得方法命名空间 //str += mb.DeclaringType.Namespace + "\n"; ////取得方法类名 //str += mb.DeclaringType.Name + "\n"; //取得方法类全名 str += mb.DeclaringType.FullName + "."; //取得法名 str += mb.Name + "()"; return str; } public static void Write(string msgText) { Write(DateTime.Now, MsgType.Information, "", msgText); } public static void Write(string msgLocation, string msgText) { Write(DateTime.Now, MsgType.Information, msgLocation, msgText); } public static void Write(MsgType msgType, string msgLocation, string msgText) { Write(DateTime.Now, msgType, msgLocation, msgText); } /// <summary> /// 基础方法 /// </summary> /// <param name="msgDataTime"></param> /// <param name="msgType"></param> /// <param name="msgLocation"></param> /// <param name="msgText"></param> public static void Write(DateTime msgDataTime, MsgType msgType, string msgLocation, string msgText) { new WriteStrToFileDelegate(WriteMsgToFile).BeginInvoke(new Msg(msgDataTime, msgType, msgLocation, msgText), null, null); } #endregion #region Msg /// <summary> /// 表示一个日志记录的对象 /// </summary> private class Msg { //日志记录的时间 private DateTime datetime; //日志记录的类型 private MsgType type; // 日志位置 private string location; //日志记录的内容 private string text; /// <summary> /// 创建新的日志记录实例; /// </summary> /// <param name="msgDataTime"></param> /// <param name="msgType"></param> /// <param name="msgLocation"></param> /// <param name="msgText"></param> public Msg(DateTime msgDataTime, MsgType msgType, string msgLocation, string msgText) { datetime = msgDataTime; type = msgType; location = msgLocation; text = msgText; } /// <summary> /// 获取或设置日志记录的时间 /// </summary> public DateTime Datetime { get { return datetime; } set { datetime = value; } } public string Location { get { return location; } set { location = value; } } /// <summary> /// 获取或设置日志记录的消息类型 /// </summary> public MsgType Type { get { return type; } set { type = value; } } /// <summary> /// 获取或设置日志记录的文本内容 /// </summary> public string Text { get { return text; } set { text = value; } } } #endregion #region MsgType /// <summary> /// 日志消息类型的枚举 /// </summary> public enum MsgType { /// <summary> /// 普通信息 /// </summary> Information, /// <summary> /// 指示警告信息类型的日志记录 /// </summary> Warning, /// <summary> /// 指示错误信息类型的日志记录 /// </summary> Error, /// <summary> /// 指示成功信息类型的日志记录 /// </summary> Success, /// <summary> /// 指示致命类型的日志记录 /// </summary> Fatal } #endregion }}
- 非常好用的异步日志记录类
- 记录一下非常好用的截图软件
- 王巧乐菇凉的360图书馆--记录大量web日志分析的内容,非常好
- 非常好用的前端工具集合,做个记录
- 记录php中非常好用的兼容性换行符PHP_EOL
- mfc一个日志库,非常好用,nice
- 非常好用的zend framework 分页类
- 非常好用的串口读写类
- C++实现的Tuple类,非常好用
- 非常好用的大数类模板 Bignum
- 《Android网络请求篇》MyHttpUtils一个非常好用的异步网络请求框架
- 简单的日志记录类。
- 日志类的学习记录
- 记录日志的工具类
- 日志框架Nlog之异步记录
- logback高级特性,异步记录日志
- logback高级特性,异步记录日志
- 为什么 Node.js 的异步 IO 具有非常好的性能
- dfgr
- 论文阅读——Chord原理
- 理解Linux中断 (2)
- 用户注册__浏览器+服务器__HTML+CSS+JS+JAVA
- MFC中调用UpdateAllViews()无效问题
- 非常好用的异步日志记录类
- JavaScript继承方法总结
- 程序员的奋斗史(一)——浅谈几种主要编程语言
- Oracle10g EM乱码之快速解决
- System Information for Windows,很不错的工具软件
- 我的时间管理--无压学习,快乐生活
- VC++中对文件的写入和读取
- html select 跳转
- Fortran函数(1):子程序、函数——出自《Fortran 95 程序设计》 彭国伦