下面以一个类便是对发布器进行配置操作,如获取上述三个文件的位置并设置相关配置:
///
1<summary>
2
3/// ExceptionSetting 的摘要说明。
4
5/// </summary>
public class ExceptionSetting
{
///
1<summary>
2
3/// 获取用于异常处理的文件夹
4
5/// </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;
}
}
///
1<summary>
2
3/// 获取或设置用于异常处理的文件夹的字符串
4
5/// </summary>
public static string FilePathSettingString
{
get
{
return AppConfig.GetAppSetting("ExceptionPath");
}
set
{
AppConfig.SaveAppSetting("ExceptionPath",value);
}
}
///
1<summary>
2
3/// 获取或设置系统异常记录类型,默认为使用XML文件记录
4
5/// </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());
}
}
}
而下面这一个类则是用于对异常发布器用到的三个文件进行读写的:
///
1<summary>
2
3/// LogAccess 的摘要说明。
4
5/// </summary>
public class LogAccess
{
///
1<summary>
2
3/// 写入异常处理日志
4
5/// </summary>
///
1<param name="ds"/>
public static void WriteLogFile(ExceptionLogData ds)
{
ds.WriteXml(ExceptionSetting.FilePath +"ExceptionLog.XML");
}
///
1<summary>
2
3/// 读出异常处理日志
4
5/// </summary>
///
1<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;
}
///
1<summary>
2
3/// 读出自定义通用信息
4
5/// </summary>
///
1<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;
}
///
1<summary>
2
3/// 写入自定义通用信息
4
5/// </summary>
///
1<param name="ds"/>
public static void SaveCustomOutMessage(CustomOutMessageData ds)
{
ds.WriteXml(ExceptionSetting.FilePath +"CustomOutMessage.XML");
}
///
1<summary>
2
3/// 获取异常处理定义信息
4
5/// </summary>
///
1<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
");
w.Flush();
w.Close();
}
exceptionDetailDS.Clear();
exceptionDetailDS.ReadXml(ExceptionSetting.FilePath +"ExceptionList.xml");
return exceptionDetailDS;
}
///
1<summary>
2
3/// 保存异常处理定义信息
4
5/// </summary>
///
1<param name="exceptionDetailDS"/>
要保存的异常处理定义信息
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
");
w.Flush();
w.Close();
}
}
(未完待续)