CSDN博客

img lornshrimp

.Net异常发布器的开发(2)

发表于2004/2/10 12:16:00  839人阅读

下面以一个类便是对发布器进行配置操作,如获取上述三个文件的位置并设置相关配置:
/// <summary>
    /// ExceptionSetting 的摘要说明。
     /// </summary>
     public class ExceptionSetting
     {
         /// <summary>
         /// 获取用于异常处理的文件夹
         /// </summary>
         public static string FilePath
         {
              get
              {
                   string fullpath;
                   if (AppConfig.GetAppSetting("ExceptionPath") != null)
                   {
                       fullpath = AppConfig.GetAppSetting("ExceptionPath");
                       while (fullpath.StartsWith("//"))
                       {
                            fullpath = fullpath.Remove(0,1);
                       }
                   }
                   else
                   {
                       fullpath = Path.GetDirectoryName(Path.GetFullPath("Temp.XML"));
                   }
                   fullpath = Path.GetFullPath(fullpath);
                   fullpath = fullpath + "//";
                   if (!Directory.Exists(Path.GetDirectoryName(fullpath)))
                   {
                       Directory.CreateDirectory(Path.GetDirectoryName(fullpath));
                   }
                   return fullpath;
              }
         }
 
         /// <summary>
         /// 获取或设置用于异常处理的文件夹的字符串
         /// </summary>
         public static string FilePathSettingString
         {
              get
              {
                   return AppConfig.GetAppSetting("ExceptionPath");
              }
              set
              {
                   AppConfig.SaveAppSetting("ExceptionPath",value);
              }
         }
 
         /// <summary>
         /// 获取或设置系统异常记录类型,默认为使用XML文件记录
         /// </summary>
         public static CanUseExceptionLogType ExceptionLogType
         {
              get
              {
                   string exceptionLogType = AppConfig.GetAppSetting("ExceptionLogType");
                   if (exceptionLogType.ToLower().Trim() == CanUseExceptionLogType.SystemLog.ToString().ToLower())
                   {
                       return CanUseExceptionLogType.SystemLog;
                   }
                   else if (exceptionLogType.ToLower().Trim() == CanUseExceptionLogType.All.ToString().ToLower())
                   {
                       return CanUseExceptionLogType.All;
                   }
                   else
                       return CanUseExceptionLogType.XMLFile;
              }
              set
              {
                   AppConfig.SaveAppSetting("ExceptionLogType",value.ToString());
              }
         }
}
 
而下面这一个类则是用于对异常发布器用到的三个文件进行读写的:
/// <summary>
     /// LogAccess 的摘要说明。
     /// </summary>
     public class LogAccess
     {
         /// <summary>
         /// 写入异常处理日志
         /// </summary>
         /// <param name="ds"></param>
         public static void WriteLogFile(ExceptionLogData ds)
         {
              ds.WriteXml(ExceptionSetting.FilePath +"ExceptionLog.XML");
         }
 
         /// <summary>
         /// 读出异常处理日志
         /// </summary>
         /// <returns></returns>
         public static ExceptionLogData ReadLogFile()
         {
              ExceptionLogData ds = new ExceptionLogData();
              if ( !File.Exists(ExceptionSetting.FilePath +"ExceptionLog.XML"))
              {
                   ds.WriteXml(ExceptionSetting.FilePath +"ExceptionLog.XML");
              }
              ds.Clear();
              ds.ReadXml(ExceptionSetting.FilePath +"ExceptionLog.XML");
              return ds;
         }
 
         /// <summary>
         /// 读出自定义通用信息
         /// </summary>
         /// <returns></returns>
         public static CustomOutMessageData GetCustomOutMessage()
         {
              CustomOutMessageData ds = new CustomOutMessageData();
              if ( !File.Exists(ExceptionSetting.FilePath +"CustomOutMessage.XML"))
              {
                   ds.WriteXml(ExceptionSetting.FilePath +"CustomOutMessage.XML");
              }
              ds.Clear();
              ds.ReadXml(ExceptionSetting.FilePath +"CustomOutMessage.XML");
              return ds;
         }
 
         /// <summary>
         /// 写入自定义通用信息
         /// </summary>
         /// <param name="ds"></param>
         public static void SaveCustomOutMessage(CustomOutMessageData ds)
         {
              ds.WriteXml(ExceptionSetting.FilePath +"CustomOutMessage.XML");
         }
 
         /// <summary>
         /// 获取异常处理定义信息
         /// </summary>
         /// <returns>读取到的异常处理定义信息</returns>
         public static ExceptionDetailData GetExceptionDetail()
         {
              ExceptionDetailData exceptionDetailDS = new ExceptionDetailData();
              if ( !File.Exists(ExceptionSetting.FilePath +"ExceptionList.xml"))
              {
                   exceptionDetailDS.WriteXml(ExceptionSetting.FilePath +"ExceptionList.xml");
                   FileStream fs = new FileStream(ExceptionSetting.FilePath +"ExceptionList.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                   StreamWriter w = new StreamWriter(fs);
                   w.BaseStream.Seek(0, SeekOrigin.End);
                   w.Write("/n/n<!--/nType:异常的类型,系统依据此值和下面的InMessage一起判定异常的处理方式,此值与异常类型的完全限定名严格相等,不区分大小写;/nInMessage:异常信息中的关键字,系统依据此关键字和上面的异常类型一起判定异常的处理方式,关键字最多可以有四个,不区分大小写,有层次关系;/nHelpLink:自定义的有关异常的帮助文件路径;/nOutMessage:自定义的向用户显示的友好信息。/n-->");
                   w.Flush();
                   w.Close();
              }
              exceptionDetailDS.Clear();
              exceptionDetailDS.ReadXml(ExceptionSetting.FilePath +"ExceptionList.xml");
              return exceptionDetailDS;
         }
 
         /// <summary>
         /// 保存异常处理定义信息
         /// </summary>
         /// <param name="exceptionDetailDS">要保存的异常处理定义信息</param>
         public static void SaveExceptionDetail(ExceptionDetailData exceptionDetailDS)
         {
              exceptionDetailDS.WriteXml(ExceptionSetting.FilePath +"ExceptionList.xml");
              FileStream fs = new FileStream(ExceptionSetting.FilePath +"ExceptionList.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
              StreamWriter w = new StreamWriter(fs);
              w.BaseStream.Seek(0, SeekOrigin.End);
              w.Write("/n/n<!--/nType:异常的类型,系统依据此值和下面的InMessage一起判定异常的处理方式,此值与异常类型的完全限定名严格相等,不区分大小写;/nInMessage:异常信息中的关键字,系统依据此关键字和上面的异常类型一起判定异常的处理方式,关键字最多可以有四个,不区分大小写,有层次关系;/nHelpLink:自定义的有关异常的帮助文件路径;/nOutMessage:自定义的向用户显示的友好信息。/n-->");
              w.Flush();
              w.Close();
         }
}
 
(未完待续)
0 0

相关博文

我的热门文章

img
取 消
img