[WPF]C#日志Log记录类,可分开日志类型记录
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2017-08-19 17:42:44
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
使用方法
Log m_log = Log.getInstance(); m_log.write("写日志测试"); m_log.write("写日志测试","debug");
生成文件
日志记录格式
Log.cs 类
using System; using System.Text; using System.IO; using System.Collections; namespace Ank.Class { public class Log { private static Log m_log = null; private string m_LogFilePath = ""; private string m_logPath = ""; private string m_logFilePrefix = ""; private Hashtable fosws = null; private Log(string filePath, string logFilePrefix) { m_logPath = filePath; m_logFilePrefix = logFilePrefix; fosws = new Hashtable(); } public static Log getInstance(string filePath=null, string logFilePrefix="log") { // if (m_log == null) { if (filePath == null) { filePath = System.AppDomain.CurrentDomain.BaseDirectory==null?"c://": System.AppDomain.CurrentDomain.BaseDirectory; } m_log = new Log(filePath, logFilePrefix); } return m_log; } public void write(string logstr,string logtype="all") { string logTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); write(logTime, logstr,logtype); logstr = null; } public void write(string logTime, string logstr,string logtype="all") { logtype = logtype.ToLower(); try { //一小时写一个文件 string strNowY = DateTime.Now.Year.ToString(); string strNowM = DateTime.Now.Month.ToString(); string strNowD = DateTime.Now.Day.ToString(); string strNowH = DateTime.Now.Hour.ToString(); string fileName = m_logFilePrefix + "_" + strNowH + "0000_"+logtype+".log"; string filePath = m_logPath + "\\logs\\" + strNowY + "-" + strNowM + "-" + strNowD + "\\"; if (logTime != "") { logTime = "[" + logTime + "] "; } //LOG目录不存在,则创建 if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } m_LogFilePath = filePath + fileName; //日志文件不存在,则创建 if (File.Exists(filePath + fileName) == false) { File.Create(filePath + fileName).Close(); } StreamWriter fosw=null; if (fosws.Contains(logtype)) { fosw = (StreamWriter)fosws[logtype]; } else { fosw = new StreamWriter(filePath + fileName, true, Encoding.UTF8); fosws.Add(logtype,fosw); }; //创建实例 if (fosw == null) { fosw = new StreamWriter(filePath + fileName, true, Encoding.UTF8); fosws.Add(logtype, fosw); } //将内容写到log文件中 fosw.WriteLine(logTime + logstr); //刷新,实时保存 fosw.Flush(); } catch (Exception ex) { System.Diagnostics.Debug.Print("Log Error:" + ex.Message.ToString()); } } } }