在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在编写应用程序时,经常会记录一些日志,于是写了这个日志类。 功能简单,不详述,自己看代码。 代码: 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 启动 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论