asp.net 2.0 有可以配置,程序出错提示就保存到指定的文件的配置方法吗?

shuhuo 2008-05-09 05:36:12
asp.net 2.0 有可以配置,程序出错提示就保存到指定的文件的配置方法吗?

就是不用通过每个页面写程序(如果出错就记录到一个TXT文件里),可以通过WEB.CONFIG或其它方式

因为想解决有时修改上传后,程序出错了,却不知道的
...全文
130 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JL99000 2008-05-11
  • 打赏
  • 举报
回复
首先 用Global.asax中的Application_Error解决的是应用程序错误,他包括所有的应用程序级别的错误,因此这种方法是不详细的,
也是不具体的,建议不用这中方法

其次 要想追踪错误的发生点,应该用写日志文件的方式,专门写个方法,用于写错误日志,这个是比较常见的解决方法
像下边的代码:
private static string ErrorPath = ConfigurationManager.AppSettings["ErrorPath"].ToString();
//上面是读取web.config中的错误路径
public static void Write(string message)
{
DateTime now = DateTime.Now;
int day_now = now.Day;
string path = "";
if (day_now == 1)
{
path = ErrorPath + Convert.ToString(DateTime.Now.Year) + "-" + Convert.ToString(DateTime.Now.Month - 1) + ".txt";
FileInfo fi = new FileInfo(path);
fi.Delete();
}
path = ErrorPath + Convert.ToString(DateTime.Now.Year) + "-" + Convert.ToString(DateTime.Now.Month) + ".txt";
StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine(message);
sw.Close();
}
--
这么做的好处是看指定日期的错误日志即可知道该天是否发生了错误,错误的内容是什么
LoveCherry 2008-05-10
  • 打赏
  • 举报
回复
For our projects, we use an httpmodule to handle all unhandled exceptions. This module will log a lot of useful information such as exception info, user info and web server info into database automatically. And we could then view or search all this information daily to find out if there is a security problem, performance problem or bug. Demo code of httpmodule is:
public class ExceptionModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.Error += new EventHandler(OnError);
}

public void OnError(object sender, EventArgs args)
{
HttpApplication application = (HttpApplication)sender;
te_ExceptionLog log = GetExceptionLog(application.Server.GetLastError(), application.Context); // form exception entity in another method
string ExceptionProcessWay = ConfigurationManager.AppSettings["ExceptionProcessWay"]; // how to handle exception? log into db or send mail?
IExceptionProcess server = (IExceptionProcess)Activator.GetObject(typeof(IExceptionProcess), ConfigurationManager.AppSettings["ETSRemotingURL"].ToString());
server.LogException(log, ExceptionProcessWay); // we use .net remoting to access database, you can directly use ado.net here
}
}
Should be aware that, you still need to use customErrors configuration to direct user to a friendly page.
BTW, you can also have a look at ASP.NET 2.0 health monitoring.
hery2002 2008-05-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 shuhuo 的帖子:]
asp.net 2.0 有可以配置,程序出错提示就保存到指定的文件的配置方法吗?
就是不用通过每个页面写程序(如果出错就记录到一个TXT文件里),可以通过WEB.CONFIG或其它方式
因为想解决有时修改上传后,程序出错了,却不知道的
[/Quote]
用log可以么?
或者是自己写个函数,写入文件就可以了啥.
gongsun 2008-05-10
  • 打赏
  • 举报
回复
2楼的很ok...
nhconch 2008-05-10
  • 打赏
  • 举报
回复
发email:
Dim Mail As MailMessage = New MailMessage(dr("EMailFrom").ToString, dr("EMailTo").ToString)
Mail.Subject = dr("EMailSubject")
Mail.Body = dr("ExceptEMailBody")
Dim SMTP As SmtpClient = New SmtpClient()
SMTP.Host = dr("EMailSMTP")
SMTP.Credentials = New System.Net.NetworkCredential(dr("EMailAccount"), dr("EMailPassword"))
SMTP.Send(Mail)
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
添加新项-全局应用程序类-打开Global.asax-输入2楼的代码就可以了
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
用Global.asax

void Application_Error(object sender, EventArgs e) 
{
Exception objErr = Server.GetLastError().GetBaseException();
Response.Write(objErr.Message);
Server.ClearError();
}
zengxie 2008-05-09
  • 打赏
  • 举报
回复
你可以把错误信息以邮件的方式发给你

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧