代码
二、在Global.asax文件中添加应用出错代码,写入系统日志文件 <customErrors mode="On" defaultRedirect="/error/customerrorpage.aspx"> //Off则关闭
<error statusCode="404" redirect="/error/404Page.aspx"/>
<error statusCode="403" redirect="/error/403page.aspx"/>
</customErrors>
<error statusCode="404" redirect="/error/404Page.aspx"/>
<error statusCode="403" redirect="/error/403page.aspx"/>
</customErrors>
代码
三、我在Default.aspx.cs中产生一个错误,默认的错误页面!protected void Application_Error(Object sender, EventArgs e)
{
Exception LastError = Server.GetLastError();
String ErrMessage = LastError.ToString();
String LogName = "MyLog";
String Message = "Url " + Request.Path + " Error: " + ErrMessage;
// 创建日志事件名
if (!EventLog.SourceExists(LogName))
{
EventLog.CreateEventSource(LogName, LogName);
}
EventLog Log = new EventLog();
Log.Source = LogName; //日志的名称,将会显示在事件查看器中
//以下是5个事件
Log.WriteEntry(Message, EventLogEntryType.Information, 1);
Log.WriteEntry(Message, EventLogEntryType.Error, 2);
Log.WriteEntry(Message, EventLogEntryType.Warning, 3);
Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4);
Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5);
}
{
Exception LastError = Server.GetLastError();
String ErrMessage = LastError.ToString();
String LogName = "MyLog";
String Message = "Url " + Request.Path + " Error: " + ErrMessage;
// 创建日志事件名
if (!EventLog.SourceExists(LogName))
{
EventLog.CreateEventSource(LogName, LogName);
}
EventLog Log = new EventLog();
Log.Source = LogName; //日志的名称,将会显示在事件查看器中
//以下是5个事件
Log.WriteEntry(Message, EventLogEntryType.Information, 1);
Log.WriteEntry(Message, EventLogEntryType.Error, 2);
Log.WriteEntry(Message, EventLogEntryType.Warning, 3);
Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4);
Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5);
}
代码
private void Page_Load(object sender, System.EventArgs e)
{
try
{
int y=0;
int x=1/y;
}
catch (Exception Err)
{
throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode。
}
}
{
try
{
int y=0;
int x=1/y;
}
catch (Exception Err)
{
throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode。
}
}