public class WriteLog { /// /// 創建日誌文件 /// /// 異常類 public static void CreateLog(Exception ex) { string path = A... ...
public class WriteLog
{
/// <summary>
/// 創建日誌文件
/// </summary>
/// <param name="ex">異常類</param>
public static void CreateLog(Exception ex)
{
string path = Application.StartupPath+"\\log";
if (!Directory.Exists(path))
{
//創建日誌文件夾
Directory.CreateDirectory(path);
}
//發生異常每天都創建一個單獨的日子文件[*.log],每天的錯誤信息都在這一個文件里。方便查找
path += "\\"+DateTime.Now.ToShortDateString() + ".log";
WriteLogInfo(ex, path);
}
/// <summary>
/// 寫日誌信息
/// </summary>
/// <param name="ex">異常類</param>
/// <param name="path">日誌文件存放路徑</param>
private static void WriteLogInfo(Exception ex, string path)
{
using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
{
sw.WriteLine("*****************************************【"
+ DateTime.Now.ToLongTimeString()
+ "】*****************************************");
if (ex != null)
{
sw.WriteLine("【ErrorType】" + ex.GetType());
sw.WriteLine("【TargetSite】" + ex.TargetSite);
sw.WriteLine("【Message】" + ex.Message);
sw.WriteLine("【Source】" + ex.Source);
sw.WriteLine("【StackTrace】" + ex.StackTrace);
}
else
{
sw.WriteLine("Exception is NULL");
}
sw.WriteLine();
}
}
}