C#實現的簡單實用日誌

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace LogServcie
{
    public static  class Log
    {
        public static string LOGMSGPATH ="LogMsg";
        public static string ERRMSGPATH = "ErrMsg";
      
        /// <summary>
        /// 用於記錄用戶的操作流程並記錄下操作時間
        /// </summary>
        /// <param name="Logmsg"></param>
        public static void writeLog(string Logmsg)
        {
            string path = System.Environment.CurrentDirectory + "//" + LOGMSGPATH;
            CreaterPath(path);

            using (StreamWriter sw = new StreamWriter(path + "//log" + DateTime.Now.ToShortDateString() + ".txt", true, Encoding.Default))
            {
               // Add some text to the logfile.
                sw.WriteLine(Logmsg+"   "+DateTime.Now);
            }
        }

        /// <summary>
        /// 記錄系統運行過程中出現的錯誤
        /// </summary>
        /// <param name="Errmsg"></param>
        /// <param name="e"></param>
        public static void WirteErr(string Errmsg,Exception e)
        {

            string path = System.Environment.CurrentDirectory + "//" + ERRMSGPATH;

            CreaterPath(path);

            using (StreamWriter sw = new StreamWriter(path + "//err"+ DateTime.Now.ToShortDateString() + ".txt", true, Encoding.Default))
            {
                // Add some text to the errfile.
                sw.WriteLine(Errmsg);
                sw.WriteLine("出錯原因: "+e.Message);
                sw.Close();
            }
        }

        /// <summary>
        /// 創建制定的文檔路徑
        /// </summary>
        /// <param name="path"></param>
        private static void CreaterPath(string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }
        }

   
    }
}

 

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