您的位置:首页 > 编程语言 > ASP

捕获asp.net下的未处理异常

2015-09-17 13:02 706 查看
 Apache与Tomcat的区别 
ihttpModule vs ihttpHandler 
内部机制:
1.ihttpModule(粘合剂,侦听application事件或用户自定义事件。)
2.ihttpHandler(可以自定义对特定资源进行处理)
2.ihttpHandlerFactory(可以用来进行在自定义ihttpmodule或标准的ihttpModule之间进行选择)

ihttpHandler拦截httpRequest.
ihttpModule是事件侦听器(其作用跟glob.aspx有些相同)

捕获asp.net下的未处理异常 
对软件开发,有个基本的要求,就是要屏蔽掉未处理的异常,在系统出错的时候显示一个友好的界面给用户,同时,保存错误信息到日志中。在asp.net下,未处理的异常会引发HttpApplication.Error 事件,因此,我们可以很简单地实现这个feature。
1、写一个类实现IHttpModule接口,挂接该事件,在事件处理中执行log的功能;
2、在web.config中的customErrors配置节,指定defaultRedirect,并置mode的值为On。

 

      <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>

asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨

一,在Web.config中填写出错时显示的页面,可以根据不同的statusCode显示不同的出错页面。
   <customErrors mode="On"  //如果设置为Off则出错只返回错误信息,不会跳到自己的指定页面defaultRedirect="/error/customerrorpage.aspx">
    <error statusCode="404" redirect="/error/404Page.aspx"/>
    <error statusCode="403" redirect="/error/403page.aspx"/>
  </customErrors> 

二,在Global.asax文件中添加应用出错代码,写入系统日志文件
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;

            // Create Event Log if It Doesn't Exist
        
            if (!EventLog.SourceExists(LogName))
            {
                EventLog.CreateEventSource(LogName, LogName);
            }
            EventLog Log = new EventLog();
            Log.Source  = LogName;
            //These are the five options that will display a different icon.
            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);

        }
三,现在你可以进行测试了。
我在Default.aspx.cs中产生一个错误,果然跳到默认的错误页面!
private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            try
            {
                int y=0;
                int x=1/y;
            }
            catch (Exception Err)
            {
                throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode,该如何实现?
                //Err.
            }

9.获取错误信息并到指定页面

  不要使用Response.Redirect,而应该使用Server.Transfer 

  e.g 

// in global.asax 
protected void Application_Error(Object sender, EventArgs e) { 
if (Server.GetLastError() is HttpUnhandledException) 
Server.Transfer("MyErrorPage.aspx"); 

//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :) 

  Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 

11.自定义异常处理

//自定义异常处理类 
using System; 
using System.Diagnostics; 

namespace MyAppException 

 /// <summary> 
 /// 从系统异常类ApplicationException继承的应用程序异常处理类。 
 //
a4b6
/ 自动将异常内容记录到Windows NT/2000的应用程序日志 
 /// </summary> 
 public class AppException:System.ApplicationException 
 { 
  public AppException() 
  { 
   if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。"); 
  } 

 public AppException(string message) 
 { 
  LogEvent(message); 
 } 

 public AppException(string message,Exception innerException) 
 { 
  LogEvent(message); 
  if (innerException != null) 
  { 
   LogEvent(innerException.Message); 
  } 
 } 

 //日志记录类 
 using System; 
 using System.Configuration; 
 using System.Diagnostics; 
 using System.IO; 
 using System.Text; 
 using System.Threading; 

 namespace MyEventLog 
 { 
  /// <summary> 
  /// 事件日志记录类,提供事件日志记录支持 
  /// <remarks> 
  /// 定义了4个日志记录方法 (error, warning, info, trace) 
  /// </remarks> 
  /// </summary> 
  public class ApplicationLog 
  { 
   /// <summary> 
   /// 将错误信息记录到Win2000/NT事件日志中 
   /// <param name="message">需要记录的文本信息</param> 
   /// </summary> 
   public static void WriteError(String message) 
   { 
    WriteLog(TraceLevel.Error, message); 
   } 

   /// <summary> 
   /// 将警告信息记录到Win2000/NT事件日志中 
   /// <param name="message">需要记录的文本信息</param> 
   /// </summary> 
   public static void WriteWarning(String message) 
   { 
    WriteLog(TraceLevel.Warning, message);   
   } 

   /// <summary> 
   /// 将提示信息记录到Win2000/NT事件日志中 
   /// <param name="message">需要记录的文本信息</param> 
   /// </summary> 
   public static void WriteInfo(String message) 
   { 
    WriteLog(TraceLevel.Info, message); 
   } 
   /// <summary> 
   /// 将跟踪信息记录到Win2000/NT事件日志中 
   /// <param name="message">需要记录的文本信息</param> 
   /// </summary> 
   public static void WriteTrace(String message) 
   { 
    WriteLog(TraceLevel.Verbose, message); 
   } 

   /// <summary> 
   /// 格式化记录到事件日志的文本信息格式 
   /// <param name="ex">需要格式化的异常对象</param> 
   /// <param name="catchInfo">异常信息标题字符串.</param> 
   /// <retvalue> 
   /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para> 
   /// </retvalue> 
   /// </summary> 
   public static String FormatException(Exception ex, String catchInfo) 
   { 
    StringBuilder strBuilder = new StringBuilder(); 
    if (catchInfo != String.Empty) 
    { 
     strBuilder.Append(catchInfo).Append("/r/n"); 
    } 
    strBuilder.Append(ex.Message).Append("/r/n").Append(ex.StackTrace); 
    return strBuilder.ToString(); 
   } 

   /// <summary> 
   /// 实际事件日志写入方法 
   /// <param name="level">要记录信息的级别(error,warning,info,trace).</param> 
   /// <param name="messageText">要记录的文本.</param> 
   /// </summary> 
   private static void WriteLog(TraceLevel level, String messageText) 
   { 
    try 
    { 
     EventLogEntryType LogEntryType; 
     switch (level) 
     { 
      case TraceLevel.Error: 
       LogEntryType = EventLogEntryType.Error; 
       break; 
      case TraceLevel.Warning: 
       LogEntryType = EventLogEntryType.Warning; 
       break; 
      case TraceLevel.Info: 
       LogEntryType = EventLogEntryType.Information; 
       break; 
      case TraceLevel.Verbose: 
       LogEntryType = EventLogEntryType.SuccessAudit; 
       break; 
      default: 
       LogEntryType = EventLogEntryType.SuccessAudit; 
       break; 
     } 

     EventLog eventLog = new EventLog("Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName ); 
     //写入事件日志 
     eventLog.WriteEntry(messageText, LogEntryType); 

    } 
   catch {} //忽略任何异常 
  } 
 } //class ApplicationLog 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: