2019年12月3日 星期二

寫一個LOGS檔案

固定放在D:\\logs下
要先
using System.IO

        private static void writeLog(string logText)
        {
            DateTime Date = DateTime.Now;
            string todayAllStat = Date.ToString("yyyy-MM-dd HH:mm:ss");
            string todayYMD = Date.ToString("yyyyMMdd");

            if(!Directory.Exists("D:\\logs"))
            {
                Directory.CreateDirectory("D:\\logs");
            }

            File.AppendAllText("D:\\logs\\" + todayYMD + ".txt", "\r\n" + todayAllStat + " -- " + logText);
        }

檔名用年月日8位數字表示
資訊開頭先記錄詳細時間,後面再記錄發生的狀況
程式要用的時候再呼叫就好

沒有留言:

張貼留言