怎樣在程式中打日誌?
阿新 • • 發佈:2018-10-31
在平時程式設計中經常遇到需要打日誌診斷錯誤這種情況,遠端客戶機?不能遠端除錯?系統中沒有自己的日誌方法?這時候就需要一個方便的,隨時加入隨時刪除的日誌方法。雖然沒有什麼技術含量,卻是相當實用
不多說,直接上程式碼
/// <summary>
/// 寫日誌資訊
/// </summary>
public void MyWriteLog(string message)
{
{
string path = AppDomain.CurrentDomain .BaseDirectory;
path = System.IO.Path.GetDirectoryName(path) + "\\ApiLogs";
try
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string fileName = System.IO .Path.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, true);
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " -------------------");
sw.WriteLine(message);
sw.WriteLine ();
sw.Close();
}
catch
{
}
}
}