void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
string path=Server.MapPath("~/")+"/Application_Error/sApplication_Error.txt";
//string path = @"D:\Unle\www.unle.com3\www.unle.com\Application_Error\Application_Error.txt";
Exception objErr = Server.GetLastError();
do
{
string err = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + @"
-----------------------------
Error Caught in Application_Error event\n" +
"Error in:" + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString();
System.IO.File.AppendAllText(path, err);
} while ((objErr = objErr.InnerException) != null);
Server.ClearError();
//Response.Redirect("Error.aspx"); }
现在的情况是每天发现的异常都会累加显示在同一个文本中,我想每天生成一个文本保存当天的异常,这样查起来比较容易。不知该怎么改。
本人菜鸟,请各位帮忙!谢了!
{
// 在出现未处理的错误时运行的代码
string path=Server.MapPath("~/")+"/Application_Error/sApplication_Error.txt";
//string path = @"D:\Unle\www.unle.com3\www.unle.com\Application_Error\Application_Error.txt";
Exception objErr = Server.GetLastError();
do
{
string err = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + @"
-----------------------------
Error Caught in Application_Error event\n" +
"Error in:" + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString();
System.IO.File.AppendAllText(path, err);
} while ((objErr = objErr.InnerException) != null);
Server.ClearError();
//Response.Redirect("Error.aspx"); }
现在的情况是每天发现的异常都会累加显示在同一个文本中,我想每天生成一个文本保存当天的异常,这样查起来比较容易。不知该怎么改。
本人菜鸟,请各位帮忙!谢了!
{
System.IO.File.Create(Path);
}先根据时间创建一个文件,如果不存在就创建,存在就不需要了。
string path=Server.MapPath("~/Application_Error/sApplication_Error/")+DateTime.Now.ToString("yyyyMMdd")+".txt";
if (!System.IO.File.Exists(Path))
{
System.IO.File.Create(Path);
}
如 log20100514.txt
log20100515.txt
if (!System.IO.File.Exists(Path))
{
System.IO.File.Create(Path);
}
”就可以了吗?下面的代码不需要改吗?