close
寫log是很常用到的功能!以下分享~
一、功能 : 寫log
二、檔案格式:
1.txt檔
2.依據日期做為檔名
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String logPath = "C:\\xxxxxx\\log"; //Log目錄 | |
/// <summary> | |
/// 寫log | |
/// </summary> | |
/// <param name="logMsg">要寫入的log資訊</param> | |
private static void WriteLog(String logMsg) | |
{ | |
String logFileName = DateTime.Now.Year.ToString() + int.Parse(DateTime.Now.Month.ToString()).ToString("00") + int.Parse(DateTime.Now.Day.ToString()).ToString("00") + ".txt"; | |
String nowTime = int.Parse(DateTime.Now.Hour.ToString()).ToString("00") + ":" + int.Parse(DateTime.Now.Minute.ToString()).ToString("00") + ":" + int.Parse(DateTime.Now.Second.ToString()).ToString("00"); | |
if(!Directory.Exists(logPath)) | |
{ | |
//建立資料夾 | |
Directory.CreateDirectory(logPath); | |
} | |
if(!File.Exists(logFileName)) | |
{ | |
//建立檔案 | |
File.Create(logPath + "\\" + logFileName); | |
} | |
using (StreamWriter sw = File.AppendText(logPath+"\\"+logFileName)) | |
{ | |
sw.WriteLine("--執行時間 " + nowTime + "--"); | |
sw.WriteLine(logMsg); | |
sw.WriteLine(); | |
} | |
} |
全站熱搜