【學習筆記】開發編程之《日誌篇》

在項目正式上線後,如果出現錯誤,異常,崩潰等情況

我們往往第一想到的事就是查看日誌

所以日誌對於一個系統的維護是非常重要的

                                                                  ㅡㅡㅡㅡ園內大神《冰麟輕武

下面就是我自己用的一個方法,感覺還行

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Maticsoft.BLL.lhzExcSQL lhz = new BLL.lhzExcSQL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    DataTable dt = lhz.GetTalbe("select * from ad");
                    dt.Rows[0]["math"].ToString();
                }
                catch (Exception ex)
                {
                    WriteIn(Server.MapPath("log.txt"), DateTime.Now.ToString() + "返回結果" + ex.Message);
                }
            }
        }
        //寫入日誌
        public bool WriteIn(string strFilepath, string strNeirong)
        {
            bool bol = false;
            if (!File.Exists(strFilepath))//檢查文件是否存在
            {
                FileStream fs = File.Create(strFilepath);
                fs.Close();
            }
            //寫入文本
            StreamWriter sr = new StreamWriter(strFilepath, true, System.Text.Encoding.UTF8);
            try
            {
                sr.WriteLine(strNeirong);
                sr.Close();
                bol = true;
            }
            catch
            {
                bol = false;
            }
            return bol;
        }
    }
}

如下是效果圖:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章