[C#] 自定义异常处理类 →→→→→进入此内容的聊天室

来自 , 2019-10-05, 写在 C#, 查看 106 次.
URL http://www.code666.cn/view/7827d1ec
  1. //自定义异常处理类
  2. using System;
  3. using System.Diagnostics;
  4. namespace MyAppException
  5. {
  6.   /// <summary>
  7.   /// 从系统异常类ApplicationException继承的应用程序异常处理类。
  8.   /// 自动将异常内容记录到Windows NT/2000的应用程序日志
  9.   /// </summary>
  10.   public class AppException:System.ApplicationException
  11.                   
  12. {
  13.           public AppException()
  14.           
  15.         {
  16.                   if ( ApplicationConfiguration.EventLogEnabled ) LogEvent ( "出现一个未知错误。" );
  17.                   
  18.         }
  19.           public AppException ( string message )
  20.           
  21.         {
  22.                   LogEvent ( message );
  23.                   
  24.         }
  25.           public AppException ( string message,Exception innerException )
  26.           
  27.         {
  28.                   LogEvent ( message );
  29.                   if ( innerException != null )
  30.                           {
  31.                           LogEvent ( innerException.Message );
  32.                           }
  33.                   
  34.         }
  35.           //日志记录类
  36.           using System;
  37.           using System.Configuration;
  38.           using System.Diagnostics;
  39.           using System.IO;
  40.           using System.Text;
  41.           using System.Threading;
  42.           namespace MyEventLog
  43.                                   
  44.         {
  45.           /// <summary>
  46.           /// 事件日志记录类,提供事件日志记录支持
  47.           /// <remarks>
  48.           /// 定义了4个日志记录方法 (error, warning, info, trace)
  49.           /// </remarks>
  50.           /// </summary>
  51.           public class ApplicationLog
  52.                           
  53.         {
  54.                   /// <summary>
  55.                   /// 将错误信息记录到Win2000/NT事件日志中
  56.                   /// <param name="message">需要记录的文本信息</param>
  57.                   /// </summary>
  58.                   public static void WriteError ( String message )
  59.                   
  60.                 {
  61.                           WriteLog ( TraceLevel.Error, message );
  62.                           
  63.                 }
  64.                   /// <summary>
  65.                   /// 将警告信息记录到Win2000/NT事件日志中
  66.                   /// <param name="message">需要记录的文本信息</param>
  67.                   /// </summary>
  68.                   public static void WriteWarning ( String message )
  69.                   
  70.                 {
  71.                           WriteLog ( TraceLevel.Warning, message );
  72.                           
  73.                           
  74.                 }
  75.                   /// <summary>
  76.                   /// 将提示信息记录到Win2000/NT事件日志中
  77.                   /// <param name="message">需要记录的文本信息</param>
  78.                   /// </summary>
  79.                   public static void WriteInfo ( String message )
  80.                   
  81.                 {
  82.                           WriteLog ( TraceLevel.Info, message );
  83.                           
  84.                 }
  85.                   /// <summary>
  86.                   /// 将跟踪信息记录到Win2000/NT事件日志中
  87.                   /// <param name="message">需要记录的文本信息</param>
  88.                   /// </summary>
  89.                   public static void WriteTrace ( String message )
  90.                   
  91.                 {
  92.                           WriteLog ( TraceLevel.Verbose, message );
  93.                           
  94.                 }
  95.                   /// <summary>
  96.                   /// 格式化记录到事件日志的文本信息格式
  97.                   /// <param name="ex">需要格式化的异常对象</param>
  98.                   /// <param name="catchInfo">异常信息标题字符串.</param>
  99.                   /// <retvalue>
  100.                   /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para>
  101.                   /// </retvalue>
  102.                   /// </summary>
  103.                   public static String FormatException ( Exception ex, String catchInfo )
  104.                   
  105.                 {
  106.                           StringBuilder strBuilder = new StringBuilder();
  107.                           if ( catchInfo != String.Empty )
  108.                                   {
  109.                                   strBuilder.Append ( catchInfo ).Append ( "\r\n" );
  110.                                   }
  111.                           strBuilder.Append ( ex.Message ).Append ( "\r\n" ).Append ( ex.StackTrace );
  112.                           return strBuilder.ToString();
  113.                           
  114.                 }
  115.                   /// <summary>
  116.                   /// 实际事件日志写入方法
  117.                   /// <param name="level">要记录信息的级别(error,warning,info,trace).</param>
  118.                   /// <param name="messageText">要记录的文本.</param>
  119.                   /// </summary>
  120.                   private static void WriteLog ( TraceLevel level, String messageText )
  121.                   
  122.                 {
  123.                           try
  124.                                   
  125.                         {
  126.                                   EventLogEntryType LogEntryType;
  127.                                   switch ( level )
  128.                                           
  129.                                 {
  130.                                   case TraceLevel.Error:
  131.                                           LogEntryType = EventLogEntryType.Error;
  132.                                           break;
  133.                                   case TraceLevel.Warning:
  134.                                           LogEntryType = EventLogEntryType.Warning;
  135.                                           break;
  136.                                   case TraceLevel.Info:
  137.                                           LogEntryType = EventLogEntryType.Information;
  138.                                           break;
  139.                                   case TraceLevel.Verbose:
  140.                                           LogEntryType = EventLogEntryType.SuccessAudit;
  141.                                           break;
  142.                                   default:
  143.                                           LogEntryType = EventLogEntryType.SuccessAudit;
  144.                                           break;
  145.                                           
  146.                                 }
  147.                                   EventLog eventLog = new EventLog ( "Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName );
  148.                                   //写入事件日志
  149.                                   eventLog.WriteEntry ( messageText, LogEntryType );
  150.                                   
  151.                         }
  152.                           catch {} //忽略任何异常
  153.                           
  154.                 }
  155.           
  156. } //class ApplicationLog
  157. }

回复 "自定义异常处理类"

这儿你可以回复上面这条便签

captcha