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

📄 configfile.cs

📁 监控系统
💻 CS
字号:
using System;
using System.Configuration;
using System.IO;
using MonitorSystem.BasicClass;

namespace MonitorSystem.MonitorInterface
{
	/// <summary>
	/// 平台配置信息.
	/// </summary>
	public class ConfigFile
	{
		/// <summary>
		/// 平台ID
		/// </summary>
		public int PlatformID;
		/// <summary>
		/// 平台名称
		/// </summary>
		public string PlatformName;
		/// <summary>
		/// 平台说明
		/// </summary>
		public string PlatformRemark;
		/// <summary>
		/// 平台节点ID
		/// </summary>
		public int[] NodeID;
		/// <summary>
		/// 监控中心IP地址
		/// </summary>
		public string CenterIP;
		/// <summary>
		/// 监控中心FTP端口
		/// </summary>
		public int CenterPort;
		/// <summary>
		/// 监控中心FTP用户名
		/// </summary>
		public string CenterFtpUserName;
		/// <summary>
		/// 监控中心FTP密码
		/// </summary>
		public string CenterFtpPsw;
		/// <summary>
		/// 平台从中心下载规则文件的路径
		/// </summary>
		public string CenterRule_Path;
		/// <summary>
		/// 平台向中心上传监控日志文件的路径
		/// </summary>
		public string CenterLog_Path;
		/// <summary>
		/// 节点规则文件下载路径
		/// </summary>
		public string RuleRemote_Path;
		/// <summary>
		/// 平台规则文件临时存放路径
		/// </summary>
		public string RuleTemp_Path;
		/// <summary>
		/// 平台规则文件本地保存路径
		/// </summary>
		public string RuleLocal_Path;
		/// <summary>
		/// 节点监控日志上传路径
		/// </summary>
		public string LogRemote_Path;
		/// <summary>
		/// 已处理日志本地路径
		/// </summary>
		public string LogLocal_Path;
		/// <summary>
		/// 监控过滤器,指定监控的LOG文件后缀名
		/// </summary>
		public string LogFilter;
		/// <summary>
		/// 规则文件名的格式
		/// </summary>
		public string RuleFileNameFormat;
		/// <summary>
		/// 监控日志文件名头
		/// </summary>
		public string LogFileHead;
		/// <summary>
		/// 下载规则文件时间间隔
		/// </summary>
		public TimeSpan DownloadTimeSpan;
		/// <summary>
		/// 节点心跳检测时间间隔
		/// </summary>
		public TimeSpan NodeCheckTimeSpan;
		/// <summary>
		/// 解析线程对log文件解析失败后,重试的次数
		/// </summary>
		public int TryCount;
		/// <summary>
		/// 对log文件解析失败后,重试的时间间隔
		/// </summary>
		public TimeSpan TryReadTimeSpan;

		private SystemLog m_SysLog=new SystemLog();

		public ConfigFile()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		/// <summary>
		/// 初始化中心接口系统配置信息
		/// </summary>
		/// <returns></returns>
		public int InitSysPath()
		{
			AppSettingsReader settingreader = new AppSettingsReader();

			try
			{
				PlatformID = Convert.ToInt32((string)settingreader.GetValue("MonitorInterface.PlatformID", typeof(string)));
				PlatformName = (string)settingreader.GetValue("MonitorInterface.PlatformName", typeof(string));
				PlatformRemark = (string)settingreader.GetValue("MonitorInterface.PlatformRemark", typeof(string));
				string strNodeID = (string)settingreader.GetValue("MonitorInterface.NodeID", typeof(string));
				CenterIP = (string)settingreader.GetValue("MonitorInterface.CenterIP", typeof(string));
				CenterPort = Convert.ToInt32((string)settingreader.GetValue("MonitorInterface.CenterPort", typeof(string)));
				CenterFtpUserName = (string)settingreader.GetValue("MonitorInterface.CenterFtpUserName", typeof(string));
				CenterFtpPsw = (string)settingreader.GetValue("MonitorInterface.CenterFtpPsw", typeof(string));
				CenterRule_Path = (string)settingreader.GetValue("MonitorInterface.CenterRulePath", typeof(string));
				CenterLog_Path = (string)settingreader.GetValue("MonitorInterface.CenterLogPath", typeof(string));
				RuleRemote_Path = (string)settingreader.GetValue("MonitorInterface.RuleRemotePath", typeof(string));
				RuleTemp_Path = (string)settingreader.GetValue("MonitorInterface.RuleTempPath", typeof(string));
				RuleLocal_Path = (string)settingreader.GetValue("MonitorInterface.RuleLocalPath", typeof(string));
				LogRemote_Path = (string)settingreader.GetValue("MonitorInterface.LogRemotePath", typeof(string));
				LogLocal_Path = (string)settingreader.GetValue("MonitorInterface.LogLocalPath", typeof(string));
				LogFilter=(string)settingreader.GetValue("MonitorInterface.Log.Filter", typeof(string));
				RuleFileNameFormat=(string)settingreader.GetValue("MonitorInterface.RuleFileNameFormat", typeof(string));
				LogFileHead=(string)settingreader.GetValue("MonitorInterface.LogFileHead", typeof(string));
				DownloadTimeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble(settingreader.GetValue("MonitorInterface.DownloadRule.TimeInterval", typeof(string))));
				NodeCheckTimeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble(settingreader.GetValue("MonitorInterface.NodeCheck.TimeInterval", typeof(string))));
				TryCount = Convert.ToInt32((string)settingreader.GetValue("MonitorInterface.ReadLog.TryCount", typeof(string)));
				TryReadTimeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble(settingreader.GetValue("MonitorInterface.ReadLog.TryReadTimeSpan",typeof(string))));

				string[] NodeIDArry = strNodeID.Split(',');
				this.NodeID = new int[NodeIDArry.Length];
				for(int n=0;n<NodeIDArry.Length;n++)
				{
					NodeID[n]=Convert.ToInt32(NodeIDArry[n]);
				}
			}
			catch(Exception e)
			{
				m_SysLog.WriteToSysLog(0,e.ToString());
				return -1;
			}
			finally
			{
				if(settingreader!=null)
					settingreader=null;
			}
			
			try
			{
				if(this.RuleRemote_Path[this.RuleRemote_Path.Length-1] != '\\')
				{
					this.RuleRemote_Path = this.RuleRemote_Path + "\\";
				}
				if(this.RuleTemp_Path[this.RuleTemp_Path.Length-1] != '\\')
				{
					this.RuleTemp_Path = this.RuleTemp_Path + "\\";
				}
				if(this.RuleLocal_Path[this.RuleLocal_Path.Length-1] != '\\')
				{
					this.RuleLocal_Path = this.RuleLocal_Path + "\\";
				}
				if(this.LogRemote_Path[this.LogRemote_Path.Length-1] != '\\')
				{
					this.LogRemote_Path = this.LogRemote_Path + "\\";
				}
				if(this.LogLocal_Path[this.LogLocal_Path.Length-1] != '\\')
				{
					this.LogLocal_Path = this.LogLocal_Path + "\\";
				}
				if(this.CenterRule_Path[this.CenterRule_Path.Length-1] != '/')
				{
					this.CenterRule_Path = this.CenterRule_Path + "/";
				}
				if(this.CenterLog_Path[this.CenterLog_Path.Length-1] != '/')
				{
					this.CenterLog_Path = this.CenterLog_Path + "/";
				}

				//如果没有目录则创建
				if(!Directory.Exists(RuleRemote_Path)) 
					Directory.CreateDirectory(RuleRemote_Path);
				if(!Directory.Exists(RuleTemp_Path)) 
					Directory.CreateDirectory(RuleTemp_Path);
				if(!Directory.Exists(RuleLocal_Path)) 
					Directory.CreateDirectory(RuleLocal_Path);
				if(!Directory.Exists(LogRemote_Path)) 
					Directory.CreateDirectory(LogRemote_Path);
				if(!Directory.Exists(LogLocal_Path)) 
					Directory.CreateDirectory(LogLocal_Path);
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(0,ex.Message);
			}

			return 0;

		}


		/// <summary>
		/// 从配置文件platform.config中获取平台配置信息.
		/// </summary>
		/// <param name="strFilePath"></param>
		/// <returns></returns>
		public int GetConfigFromFile()
		{
			FileInfo fi=null;
			FileStream fs=null;
			StreamReader sr=null;

			//string strFilePath=this.PlatCfg_Path;
			//strFilePath+=this.PlatCfg_Name;
			string strFilePath="";

			try
			{
				fi=new FileInfo(strFilePath);

				if (!fi.Exists)
				{
					throw new Exception("节点配置文件不存在");
				}

				fs = fi.OpenRead();
				sr = new StreamReader (fs,new System.Text.ASCIIEncoding());

				string line="";
				string[] delim = new string[]{"PlatformID=","PlatformName=","PlatformRemark=","NodeID=",
							"CenterIP=","CenterPort=","CenterFtpUserName=","CenterFtpPsw=","CenterRulePath=",
							"CenterLogPath=","CenterAlertPath=","RuleRemotePath=","RuleTempPath=",
							"RuleLocalPath=","LogRemotePath=","LogLocalPath="};

				for (int i=0;(line=sr.ReadLine())!=null&&i<16; i++)
				{
					switch(i)
					{
						case 0:
							PlatformID = Convert.ToInt32(line.Replace(delim[i],""));
							break;
						case 1:
							PlatformName = line.Replace(delim[i],"");
							break;
						case 2:
							PlatformRemark = line.Replace(delim[i],"");
							break;
						case 3:
							string strNodeID = line.Replace(delim[i],"");
							string[] NodeIDArry = strNodeID.Split(',');
							this.NodeID = new int[NodeIDArry.Length];
							for(int n=0;n<NodeIDArry.Length;n++)
							{
								NodeID[n]=Convert.ToInt32(NodeIDArry[n]);
							}
							break;
						case 4:
							CenterIP = line.Replace(delim[i], "");
							break;
						case 5:
							CenterPort = Convert.ToInt32(line.Replace(delim[i], ""));
							break;
						case 6:
							CenterFtpUserName = line.Replace(delim[i], "");
							break;
						case 7:
							CenterFtpPsw = line.Replace(delim[i], "");
							break;
						case 8:
							CenterRule_Path = line.Replace(delim[i], "");
							break;
						case 9:
							CenterLog_Path = line.Replace(delim[i], "");
							break;
						case 10:
							//CenterAlert_Path = line.Replace(delim[i], "");
							break;
						case 11:
							RuleRemote_Path = line.Replace(delim[i], "");
							break;
						case 12:
							RuleTemp_Path = line.Replace(delim[i],"");
							break;
						case 13:
							RuleLocal_Path = line.Replace(delim[i],"");
							break;
						case 14:
							LogRemote_Path = line.Replace(delim[i],"");
							break;
						case 15:
							LogLocal_Path = line.Replace(delim[i],"");
							break;
						default:
							break;
					}

				}
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(ex.Message);
				return -1;
			}
			finally
			{
				if(fi!=null)
					fi=null;
				if(fs!=null)
					fs.Close();
				if(sr!=null)
					sr.Close();
			}

			try
			{
				if(this.RuleRemote_Path[this.RuleRemote_Path.Length-1] != '\\')
				{
					this.RuleRemote_Path = this.RuleRemote_Path + "\\";
				}
				if(this.RuleTemp_Path[this.RuleTemp_Path.Length-1] != '\\')
				{
					this.RuleTemp_Path = this.RuleTemp_Path + "\\";
				}
				if(this.RuleLocal_Path[this.RuleLocal_Path.Length-1] != '\\')
				{
					this.RuleLocal_Path = this.RuleLocal_Path + "\\";
				}
				if(this.LogRemote_Path[this.LogRemote_Path.Length-1] != '\\')
				{
					this.LogRemote_Path = this.LogRemote_Path + "\\";
				}
				if(this.LogLocal_Path[this.LogLocal_Path.Length-1] != '\\')
				{
					this.LogLocal_Path = this.LogLocal_Path + "\\";
				}
				if(this.CenterRule_Path[this.CenterRule_Path.Length-1] != '/')
				{
					this.CenterRule_Path = this.CenterRule_Path + "/";
				}
				if(this.CenterLog_Path[this.CenterLog_Path.Length-1] != '/')
				{
					this.CenterLog_Path = this.CenterLog_Path + "/";
				}

				//如果没有目录则创建
				if(!Directory.Exists(RuleRemote_Path)) 
					Directory.CreateDirectory(RuleRemote_Path);
				if(!Directory.Exists(RuleTemp_Path)) 
					Directory.CreateDirectory(RuleTemp_Path);
				if(!Directory.Exists(RuleLocal_Path)) 
					Directory.CreateDirectory(RuleLocal_Path);
				if(!Directory.Exists(LogRemote_Path)) 
					Directory.CreateDirectory(LogRemote_Path);
				if(!Directory.Exists(LogLocal_Path)) 
					Directory.CreateDirectory(LogLocal_Path);
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(0,ex.Message);
			}

			return 0;
		}
	}
}

⌨️ 快捷键说明

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