程序中错误信息的处理办法
2007-01-29 17:52
459 查看
将错误发送给开发者
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.Mail ;
using System.Diagnostics ;
namespace WebApplicationI
...{
/**//// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
public Global()
...{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
...{
}
protected void Session_Start(Object sender, EventArgs e)
...{
}
protected void Application_BeginRequest(Object sender, EventArgs e)
...{
}
protected void Application_EndRequest(Object sender, EventArgs e)
...{
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
...{
}
protected void Application_Error(Object sender, EventArgs e)
...{
//把错误信息发送到作者
string strPageUrl = Request.Path;
Exception ErrorInfo =Server.GetLastError();
//Server.ClearError();
string strMessage = "Url:" + strPageUrl + "</br>";
strMessage = strMessage + " Error: ";
strMessage = strMessage + ErrorInfo.ToString() + "</br>";
MailMessage Mymessage = new MailMessage();
Mymessage.To = "***@***.com.cn";
Mymessage.From = "***@***.com.cn";
Mymessage.Subject = "ASP.NET Error";
Mymessage.BodyFormat = MailFormat.Text;
Mymessage.Body = strMessage;
//SmtpMail.SmtpServer = " ";
SmtpMail.Send(Mymessage);
}
protected void Session_End(Object sender, EventArgs e)
...{
}
protected void Application_End(Object sender, EventArgs e)
...{
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
}
#endregion
}
}
将错误写入系统日志
string strMessage = Server.GetLastError().Message;
//Response.Write(strMessage);
Server.ClearError();
//以下把信息写入windows日志
//要把aspnet用户添加到管理员组中,以便有写注册表权限
if(!EventLog.SourceExists("mySource"))
EventLog.CreateEventSource("mySource","myLog");
EventLog Event = new EventLog();
Event.Source = "mySource";
Event.WriteEntry( System.DateTime .Now .ToString (),EventLogEntryType.Warning);
//EventLog.Delete("myLog");
//throw new Exception("我处理不了!");
将错误信息写入输出窗口
System.Diagnostics .Debug .WriteLine( 想要输出的值);
将错误信息写入目录下的log
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.AutoFlush=true;
string strPath = string.Empty;
strPath = Page.Server.MapPath (".") ;
strPath += @"/app.log";
FileStream fs = new FileStream( strPath, FileMode.Append , FileAccess.Write );
System.Diagnostics .Trace.Listeners.Add( new System.Diagnostics .TextWriterTraceListener( fs ) );
System.Diagnostics .Trace .WriteLine("star");
System.Diagnostics .Trace .WriteLine(System.DateTime.Now.ToLongTimeString () );
System.Diagnostics .Trace .WriteLine( Page.ToString());
System.Diagnostics .Trace .WriteLine("end");
fs.Close ();
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.Mail ;
using System.Diagnostics ;
namespace WebApplicationI
...{
/**//// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
public Global()
...{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
...{
}
protected void Session_Start(Object sender, EventArgs e)
...{
}
protected void Application_BeginRequest(Object sender, EventArgs e)
...{
}
protected void Application_EndRequest(Object sender, EventArgs e)
...{
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
...{
}
protected void Application_Error(Object sender, EventArgs e)
...{
//把错误信息发送到作者
string strPageUrl = Request.Path;
Exception ErrorInfo =Server.GetLastError();
//Server.ClearError();
string strMessage = "Url:" + strPageUrl + "</br>";
strMessage = strMessage + " Error: ";
strMessage = strMessage + ErrorInfo.ToString() + "</br>";
MailMessage Mymessage = new MailMessage();
Mymessage.To = "***@***.com.cn";
Mymessage.From = "***@***.com.cn";
Mymessage.Subject = "ASP.NET Error";
Mymessage.BodyFormat = MailFormat.Text;
Mymessage.Body = strMessage;
//SmtpMail.SmtpServer = " ";
SmtpMail.Send(Mymessage);
}
protected void Session_End(Object sender, EventArgs e)
...{
}
protected void Application_End(Object sender, EventArgs e)
...{
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
}
#endregion
}
}
将错误写入系统日志
string strMessage = Server.GetLastError().Message;
//Response.Write(strMessage);
Server.ClearError();
//以下把信息写入windows日志
//要把aspnet用户添加到管理员组中,以便有写注册表权限
if(!EventLog.SourceExists("mySource"))
EventLog.CreateEventSource("mySource","myLog");
EventLog Event = new EventLog();
Event.Source = "mySource";
Event.WriteEntry( System.DateTime .Now .ToString (),EventLogEntryType.Warning);
//EventLog.Delete("myLog");
//throw new Exception("我处理不了!");
将错误信息写入输出窗口
System.Diagnostics .Debug .WriteLine( 想要输出的值);
将错误信息写入目录下的log
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.AutoFlush=true;
string strPath = string.Empty;
strPath = Page.Server.MapPath (".") ;
strPath += @"/app.log";
FileStream fs = new FileStream( strPath, FileMode.Append , FileAccess.Write );
System.Diagnostics .Trace.Listeners.Add( new System.Diagnostics .TextWriterTraceListener( fs ) );
System.Diagnostics .Trace .WriteLine("star");
System.Diagnostics .Trace .WriteLine(System.DateTime.Now.ToLongTimeString () );
System.Diagnostics .Trace .WriteLine( Page.ToString());
System.Diagnostics .Trace .WriteLine("end");
fs.Close ();
相关文章推荐
- 服务器系统日志出现“挂起模块 hungapp”错误信息处理办法
- 连接oracle时出现 ORA-12514 错误信息的原因及处理办法
- 程序打包,"错误:应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行sxstrace.exe"工具解决办法
- SQL Server 2005“错误1706。安装程序找不到需要的文件。请检查……”的处理办法
- 关于 SQL 2005 安装程序无法获取ASPNET账户的系统账户信息 错误解决办法
- IIS中预览错误问题的解决办法(HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射 )
- 解决办法:asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”(转)
- eclipse编译java程序常见错误处理办法
- 内部 .Net Framework 数据提供程序错误 30 Internal .Net Framework Data Provider error 30. 的处理办法
- 用Visual Studio2013发布.ashx(一般处理程序)时遇到的错误及解决办法
- SQL Server 2005“错误1706。安装程序找不到需要的文件。请检查……”的处理办法
- 编译器错误信息: CS1056: 意外的字符的处理办法
- 在Win7(64位)系统下运行World Wind源码程序出现“未处理BadImageFormatException”错误解决方法(另:附加信息)
- 关于VS2017编译linux程序时,错误提示信息太简单,看不出错误原因的解决办法
- 错误:程序中调用WebService返回“http 401 Access Denied”解决办法
- android 程序错误处理全局处理
- 微信小程序模板消息41003 page路径错误解决办法
- 多线程Java程序中常见错误的巧处理
- 关于ASP程序运行出现ADODB.Stream 错误 800a0bbc 写入文件失败 的解决办法!
- 关于Visual Studio 2013 编译 multi-byte character set MFC程序出现 MSB8031 错误的解决办法