我不確定這是正確的方式,但我這樣做。
創建這樣一個功能:
//Log error
static public void logError(Exception erorr)
{
//create/append file with current date as file name
string logfile = DateTime.Now.ToString("yyyyMMdd") + ".log";
logfile = Global.logpath + logfile; //logpath is a full folder path defined it the Global.cs file to hold the log files.
using (StreamWriter sw = File.AppendText(logfile))
{
sw.Write("\r\nLog Entry : ");
sw.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
sw.WriteLine(" :");
sw.WriteLine(" :Message :{0}", erorr.Message);
sw.WriteLine(" :Source :{0}", erorr.Source);
if (erorr.InnerException != null)
sw.WriteLine(" :Inner Ex:{0}", erorr.InnerException.Message);
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(erorr, true);
var stackFrame = trace.GetFrame(trace.FrameCount - 1);
sw.WriteLine(" :");
sw.WriteLine(" :Stack :");
sw.WriteLine(" :Line Number :{0}", stackFrame.GetFileLineNumber());
sw.WriteLine(" :Source File :{0}", stackFrame.GetFileName());
sw.WriteLine("-------------------------------");
}
}
然後把你計算在try ... catch
塊。像
try
{
//do my sql....
//.............
{
catch(Exception ex)
{
logError(ex);
}
查看asp.net中的日誌。 創建一個頁面並閱讀日誌文件。
注:我很樂意看到專家對此方法的評論。 謝謝 快樂編碼:)
@ user2417642:需要更多幫助嗎? –