C#自定義日志記錄
廢話不多說,直接上代碼:
很簡單:將類復(fù)制到項(xiàng)目中,最后在配置文件上配置一下:logUrl即可。 默認(rèn)保存在:項(xiàng)目/temp/log
/// <summary> /// 日志類 /// </summary> /// <remarks>Creator: v-lxh CreateTime: 2016/7/26 11:18:09</remarks> /// <Description></Description> public class Log { /// <summary> /// 寫入日志. /// </summary> /// <param name="strList">The STR list.</param> /// <remarks>Creator: v-lxh CreateTime: 2016/7/26 11:18:09</remarks> /// <Description></Description> public static void WriteLog(params object[] strList) { //判斷是否開啟日志模式 //if (!LogModel) return; if (strList.Count() == 0) return; //日志文件路徑 string strDicPath = ""; try { strDicPath = HttpContext.Current.Server.MapPath("~/temp/log/"); if (strDicPath == null || strDicPath == "") { strDicPath = System.Configuration.ConfigurationManager.AppSettings["logUrl"] + "/temp/log/"; } } catch (Exception e) { strDicPath = System.Configuration.ConfigurationManager.AppSettings["logUrl"] + "/temp/log/"; } string strPath = strDicPath + string.Format("{0:yyyy年-MM月-dd日}", DateTime.Now) + "日志記錄.txt"; if (!Directory.Exists(strDicPath)) { Directory.CreateDirectory(strDicPath); } if (!File.Exists(strPath)) { using (FileStream fs = File.Create(strPath)) { } } string str = File.ReadAllText(strPath); StringBuilder sb = new StringBuilder(); foreach (var item in strList) { sb.Append("\r\n" + DateTime.Now.ToString() + "-----" + item + ""); } File.WriteAllText(strPath, sb.ToString() + "\r\n-----z-----\r\n" + str); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:PC藍(lán)牙通信C#代碼實(shí)現(xiàn)
欄 目:C#教程
本文標(biāo)題:C#自定義日志記錄
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6244.html
您可能感興趣的文章
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10C#實(shí)現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#自定義事件監(jiān)聽實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)寫系統(tǒng)日志的方法
- 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10輕松學(xué)習(xí)C#的方法
- 01-10C#自定義DataGridViewColumn顯示TreeView
- 01-10C#實(shí)現(xiàn)的自定義郵件發(fā)送類完整實(shí)例(支持多人多附件)


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什