public static void WriteErorrLog(Exception ex)<br />{<br /> if(ex == null)<br /> return; //ex = null 返回<br /> StreamWrite write = null;<br /> DateTime dt = DateTime.Now; // 設定日誌時間<br /> string time = dt.ToString("yyyy-mm-dd HH:mm:ss")); //年-月-日 時:分:秒<br /> string LogName = "Log.log"; //日誌名稱<br /> string LogPath = "C://Error//"; //日誌存放路徑<br /> string Log = LogPath + LogName; //路徑 + 名稱<br /> if(!File.Exists(Log)) //是否存在<br /> {<br /> Directory.CreateDirectory(LogPath); //建立檔案夾<br /> write = File.CreateText(LogName); // 建立日誌<br /> }<br /> else<br /> {<br /> write = File.AppendText(Log); //追加,添加錯誤資訊;<br /> }<br /> write.WriteLine(time);<br /> write.WriteLine(ex.Message);<br /> write.WriteLine("異常資訊:"+ex.ToString());<br /> write.WriteLine("異常堆棧:"+ex.StackTrace.ToString());<br /> write.WriteLine("/r/n-----------------");<br /> write.Flush();<br /> write.Dispse();</p><p>}