創作內容

0 GP

Log代碼紀錄

作者:紅豆生南國│2020-02-08 11:49:09│巴幣:0│人氣:153
由於寫程式普遍都會用到Log,以進行儲存錯誤或者異常訊息,所以我把Log的寫法記錄起來,以後可以再使用。

static class LogUse
    {
// .net frameword 4.5的寫法
        static readonly string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "LOG";
// .net core 的寫法
static readonly string path = AppDomain.CurrentDomain.BaseDirectory + "LOG\\";

        public static void CreatFile()
        {
            // 如果沒有LOG資料夾則創建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 檢查Log檔案,如果沒有的話創建
        /// 檔名為今天日期
        /// </summary>
        public static void CreatLog()
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd");

            // LOG檔案不存在的話,建立LOG檔案
            if (!File.Exists(path + "" + date + ".txt"))
            {
                var file = File.Create(path + "" + date + ".txt");
                file.Close();
            }
        }

        /// <summary>
        /// 將紀錄寫入Log檔
        /// </summary>
        /// <param name="action">要輸入的字串</param>
        public static void WriteLog(string action)
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            action = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + action;
            FileStream fileStream = new FileStream(path + "" + date + ".txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);

            using (StreamWriter sw = new StreamWriter(fileStream))
            {
                sw.WriteLine(action);
                sw.WriteLine("=================================================");
                sw.Close();
            }
        }
    }
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4678005
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

喜歡★ms0391386 可決定是否刪除您的留言,請勿發表違反站規文字。

追蹤私訊切換新版閱覽

作品資料夾

ilove487奇幻小說連載中
《克蘇魯的黎明》0672.不用道歉看更多我要大聲說4小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】