国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

點(diǎn)滴積累【C#】---錯誤日志記錄到txt文本里。

2019-11-17 02:29:41
字體:
供稿:網(wǎng)友

點(diǎn)滴積累【C#】---錯誤日志記錄到txt文本里。

效果:

描述:將系統(tǒng)中的錯誤信息,try catch到日志里面。

代碼:

【后端代碼】

 1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.IO; 5 using System.Linq; 6 using System.Web; 7  8 namespace GetLog 9 {10     public class WriteLog11     {12         PRivate static StreamWriter streamWriter; //寫文件  13 14         public static void WriteError(string message)15         {16             try17             {18                 //DateTime dt = new DateTime();19                 string directPath = ConfigurationManager.AppSettings["LogFilePath"].ToString().Trim();    //獲得文件夾路徑20                 if (!Directory.Exists(directPath))   //判斷文件夾是否存在,如果不存在則創(chuàng)建21                 {22                     Directory.CreateDirectory(directPath);23                 }24                 directPath += string.Format(@"/{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));25                 if (streamWriter == null)26                 {27                     streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判斷文件是否存在如果不存在則創(chuàng)建,如果存在則添加。28                 }29                 streamWriter.WriteLine("***********************************************************************");30                 streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));31                 streamWriter.WriteLine("輸出信息:錯誤信息");32                 if (message != null)33                 {34                     streamWriter.WriteLine("異常信息:/r/n" + message);35                 }36             }37             finally38             {39                 if (streamWriter != null)40                 {41                     streamWriter.Flush();42                     streamWriter.Dispose();43                     streamWriter = null;44                 }45             }46         }47     }48 }

【調(diào)用】:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace GetLog{    public partial class testLog : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void btn_Click(object sender, EventArgs e)        {            try            {                int intStr=Convert.ToInt32(tb.Text);                tb2.Text ="轉(zhuǎn)換成功:" +intStr.ToString();            }            catch (Exception ex)            {                WriteLog.WriteError(ex.ToString());                throw ex;            }        }    }}

Demo下載:

http://files.VEVb.com/files/xinchun/GetLog.zip


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 哈巴河县| 安图县| 景洪市| 车险| 天门市| 桂东县| 武隆县| 阆中市| 应城市| 桂林市| 锡林郭勒盟| 阳信县| 双流县| 枣庄市| 宁德市| 瑞昌市| 舒城县| 南开区| 通河县| 曲周县| 佛学| 天峻县| 田阳县| 厦门市| 丰都县| 体育| 琼海市| 延庆县| 宜昌市| 遂宁市| 伊吾县| 大竹县| 新龙县| 淮北市| 丹东市| 金沙县| 塘沽区| 宁武县| 民县| 江陵县| 县级市|