⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exsectionhandler.cs

📁 异常处理包,通用 异常处理包,通用 异常处理包,通用
💻 CS
字号:
using System;
using System.Configuration;
using System.Xml;

namespace ExManagement.Config
{
	/// <summary>
	/// ExManager配置信息读取管理类,实现了System.Configuration.IConfigurationSectionHandler接口。
	/// 可以通过System.Configuration.ConfigurationSettings.GetConfig()来调用
	/// </summary>
	public class ExSectionHandler:IConfigurationSectionHandler
	{
		public object Create(object parent, object configContext, XmlNode section)
		{
			ExManagerConfig configEx = new ExManagerConfig();

			// 遍历ExManager的属性
			foreach(XmlAttribute attr in section.Attributes)
			{
				// 获取该节点中的属性,若是未知的属性,则抛出异常
				switch(attr.Name)
				{
					case "ErrorCodeSource":
					{
						configEx.ErrorCodeSource = (ErrorCodeSource)
							Enum.Parse(typeof(ErrorCodeSource), attr.Value);
						break;
					}
					case "ConnectionString":
					{
						configEx.ConnectionString = attr.Value;
						break;
					}
					case "DataTable":
					{
						configEx.DataTable = attr.Value;
						break;
					}
					default:
					{
						throw new ConfigurationException("不可识别的配置属性", attr);
					}
				}
			}

			//遍历ExceptionManager的所有子节点
			foreach(XmlNode node in section.ChildNodes)
			{
				ExManagerConfig.ExHandlerConfig exHandler 
					= new ExManagerConfig.ExHandlerConfig();

				if(node.Name != "ExHandler")
				{
					throw new ConfigurationException("不可识别的配置项", node);
				}
				else
				{
					//遍历ExceptionHandler节点的所有属性
					foreach(XmlAttribute attr in node.Attributes)
					{
						// 获取该节点中的属性,若是未知的属性,则抛出异常
						switch(attr.Name)
						{
							case "Name":
							{
								exHandler.Name = attr.Value;
								break;
							}
							case "ReturnMode":
							{
								exHandler.ReturnMode 
									= (ExReturnMode)
									Enum.Parse(typeof(ExReturnMode), attr.Value);
								break;
							}
							case "AlertType":
							{
								exHandler.AlertType
									= (ExAlertType)
									Enum.Parse(typeof(ExAlertType), attr.Value);
								break;
							}
							case "Type":
							{
								exHandler.Type = attr.Value;
								break;
							}
							default:
							{
								throw new ConfigurationException("不可识别的配置属性", attr);
							}
						}
					}

					exHandler.LogConfigColletion = new ExManagement.Config.ExManagerConfig.LogHandlerConfigCollection();
					//获取到ExHandler/LogHandler节点
					foreach(XmlNode childNodeNode in node.ChildNodes)
					{
						if(childNodeNode.Name != "LogHandler")
						{
							throw new ConfigurationException("不可识别的配置项", childNodeNode);
						}
						else
						{
							//处理ExceptionHandler/Handler节点的属性
							ExManagerConfig.LogHandlerConfig logConfig = new ExManagerConfig.LogHandlerConfig();
							foreach(XmlAttribute attr in childNodeNode.Attributes)
							{
								// 获取该节点中的属性,若是未知的属性,则抛出异常
								switch(attr.Name)
								{
									case "Type":
									{
										logConfig.Type = attr.Value;
										break;
									}
									case "ConnectionString":
									{
										logConfig.ConnectionString = attr.Value;
										break;
									}
									case "DataTable":
									{
										logConfig.DataTable = attr.Value;
										break;
									}
									default:
									{
										throw new ConfigurationException("不可识别的配置属性", attr);
									}
								}
							}
							exHandler.LogConfigColletion.Add(logConfig);
						}
					}
				}
				configEx.Add(exHandler);
			}
			return configEx;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -