先在web站点下新建一个ErrorLog文件夹,用来存放错误日志。
然后新建一个Global.aspx。在Application_Error事件中输入处理代码
代码
protected
void
Application_Error(
object
sender, EventArgs e)
{
Exception ex = Server.GetLastError();
string errorMSG = ex.Message;
string error = string .Empty;
string errortime = string .Empty;
string erroraddr = string .Empty;
string errorinfo = string .Empty;
string errorsource = string .Empty;
string errortrace = string .Empty;
errortime = " 发生时间: " + System.DateTime.Now.ToString();
erroraddr = " 发生异常页: " + HttpContext.Current.Request.Url.ToString();
errorinfo = " 异常信息: " + ex.Message;
errorsource = " 错误源: " + ex.Source;
errortrace = " 堆栈信息: " + ex.StackTrace;
// 独占方式,因为文件只能由一个进程写入.
System.IO.StreamWriter writer = null ;
try
{
lock ( this )
{
string FileName = DateTime.Now.ToShortDateString() + " .txt " ;
string Year = DateTime.Now.Year.ToString();
string Month = DateTime.Now.Month.ToString();
string path = HttpContext.Current.Server.MapPath( " ~/ErrorLog/ " ) + Year + " / " + Month;
// 如果目录不存在则创建
if ( ! System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
System.IO.FileInfo file = new System.IO.FileInfo(path + " / " + FileName);
writer = new System.IO.StreamWriter(file.FullName, true ); // 文件不存在就创建,true表示追加
writer.WriteLine( " 用户IP: " + HttpContext.Current.Request.UserHostAddress);
writer.WriteLine(errortime);
writer.WriteLine(erroraddr);
writer.WriteLine(errorinfo);
writer.WriteLine(errorsource);
writer.WriteLine(errortrace);
writer.WriteLine( " -------------------------------------------------------------------------------------- " );
// writer.Close();
}
}
finally
{
if (writer != null )
writer.Close();
}
}
{
Exception ex = Server.GetLastError();
string errorMSG = ex.Message;
string error = string .Empty;
string errortime = string .Empty;
string erroraddr = string .Empty;
string errorinfo = string .Empty;
string errorsource = string .Empty;
string errortrace = string .Empty;
errortime = " 发生时间: " + System.DateTime.Now.ToString();
erroraddr = " 发生异常页: " + HttpContext.Current.Request.Url.ToString();
errorinfo = " 异常信息: " + ex.Message;
errorsource = " 错误源: " + ex.Source;
errortrace = " 堆栈信息: " + ex.StackTrace;
// 独占方式,因为文件只能由一个进程写入.
System.IO.StreamWriter writer = null ;
try
{
lock ( this )
{
string FileName = DateTime.Now.ToShortDateString() + " .txt " ;
string Year = DateTime.Now.Year.ToString();
string Month = DateTime.Now.Month.ToString();
string path = HttpContext.Current.Server.MapPath( " ~/ErrorLog/ " ) + Year + " / " + Month;
// 如果目录不存在则创建
if ( ! System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
System.IO.FileInfo file = new System.IO.FileInfo(path + " / " + FileName);
writer = new System.IO.StreamWriter(file.FullName, true ); // 文件不存在就创建,true表示追加
writer.WriteLine( " 用户IP: " + HttpContext.Current.Request.UserHostAddress);
writer.WriteLine(errortime);
writer.WriteLine(erroraddr);
writer.WriteLine(errorinfo);
writer.WriteLine(errorsource);
writer.WriteLine(errortrace);
writer.WriteLine( " -------------------------------------------------------------------------------------- " );
// writer.Close();
}
}
finally
{
if (writer != null )
writer.Close();
}
}