一个用C#写的日志类
在编写应用程序时,经常会记录一些日志,于是写了这个日志类。
功能简单,不详述,自己看代码。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace TagReceiver { class LogHelper { string logFile = ""; /// <summary> /// 不带参数的构造函数 /// </summary> public LogHelper() { } /// <summary> /// 带参数的构造函数 /// </summary> /// <param name="logFile"></param> public LogHelper(string logFile) { this.logFile = logFile; } /// <summary> /// 追加一条信息 /// </summary> /// <param name="text"></param> public void Write(string text) { using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text); } } /// <summary> /// 追加一条信息 /// </summary> /// <param name="logFile"></param> /// <param name="text"></param> public void Write(string logFile, string text) { using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text); } } /// <summary> /// 追加一行信息 /// </summary> /// <param name="text"></param> public void WriteLine(string text) { text += "\r\n"; using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text); } } /// <summary> /// 追加一行信息 /// </summary> /// <param name="logFile"></param> /// <param name="text"></param> public void WriteLine(string logFile, string text) { text += "\r\n"; using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8)) { sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text); } } } }
运行结果:
[2012-08-23 13:35:07] ATIS-TAGRECEIVER-V100 启动
[2012-08-23 13:41:49] ATIS-TAGRECEIVER-V100 退出