ASP.NET错误处理
using System;
using System.Web;
namespace Water.Basis
{
public class ErrorLog
{
static ErrorLog() { }
#region WriteEntry
public static void WriteEntry()
{
Exception erroy = HttpContext.Current.Server.GetLastError().GetBaseException();
string path = HttpContext.Current.Request.PhysicalApplicationPath;
string logDir = path + "Log\\";
string LogFile = logDir + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
if (!System.IO.Directory.Exists(logDir))
{
System.IO.Directory.CreateDirectory(logDir);
}
System.IO.StreamWriter sw;
if (System.IO.File.Exists(LogFile))
{
sw = System.IO.File.AppendText(LogFile);
}
else
{
sw = System.IO.File.CreateText(LogFile);
}
sw.WriteLine("出错页面是:" + HttpContext.Current.Request.Url.ToString());
sw.WriteLine("异常信息:" + erroy.Message);
sw.WriteLine("Source:" + erroy.Source);
sw.WriteLine("StackTrace:" + erroy.StackTrace);
sw.WriteLine("发生时间:" + DateTime.Now.ToString());
sw.WriteLine();
sw.WriteLine("**********************************************************************************");
sw.WriteLine();
sw.Close();
HttpContext.Current.Server.ClearError();
}
#endregion
}
}