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

📄 readlogthread.cs

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

using MonitorSystem.BasicClass;

namespace MonitorSystem.MonitorCenter
{
	/// <summary>
	/// ReadLogThread 的摘要说明。
	/// </summary>
	public class ReadLogThread
	{
		private Thread m_Thread;
		private bool m_Pause;
		private bool m_Exit;

		private LogFileQueue m_queLogFile;
		private RecordQueue m_queRecord;
		private ConfigFile m_CfgFile;
		private SystemLog m_SysLog;
		private string m_strFileName;
		private string m_strLineData;

		private FileStream m_fstream=null;
		private StreamReader m_logReader=null;

		private string strTime = "yyyyMMddHHmmss";
		private IFormatProvider formatTime = new System.Globalization.CultureInfo("zh-CN", true);

		public void Init(ref LogFileQueue iLogFileQueue, ref RecordQueue iRecordQueue, ref ConfigFile iCfgFile)
		{
			m_queLogFile = iLogFileQueue;
			m_queRecord = iRecordQueue;
			m_CfgFile = iCfgFile;
			m_SysLog = new SystemLog();
		}

		public void Run()
		{
			while(true)
			{
				while(m_Pause)
				{
					try
					{
						Thread.Sleep(1000);
					}
					catch(Exception)
					{						
					}
					continue;
				}
				if(m_Exit)
				{
					break;
				}

				if (m_queLogFile.Count <=0)
				{
					try
					{
						Thread.Sleep(1000);
						continue;
					}
					catch(Exception)
					{
						continue;
					}
				}

				try
				{
					m_strFileName = (string)m_queLogFile.Dequeue();

					bool ReadOk=false;
					int tryCount=1;
					while(!ReadOk&&tryCount<=m_CfgFile.TryCount)
					{
						try
						{
							m_fstream = new FileStream(m_strFileName,FileMode.Open);
							ReadOk = true;
						}
						catch(Exception e)
						{
							Thread.Sleep(m_CfgFile.TryReadTimeSpan);
							m_SysLog.WriteToSysLog(1,"Center:监控日志文件读取错误,重试次数:{1}\r\n原因:{0}", e.Message,tryCount);
							tryCount++;
						}
					}

					//m_fstream = new FileStream(m_strFileName,FileMode.Open);
					m_logReader = new StreamReader (m_fstream,System.Text.Encoding.GetEncoding(936));
				    int ParentId = 0;
					while((m_strLineData = m_logReader.ReadLine() )!= null)
					{
						string[] DataArry = m_strLineData.Split('\t');
						if(DataArry.GetLength(0) > 3)
						{
							LogFile log = new LogFile();

							log.strDateTime = DateTime.ParseExact(DataArry[0].ToString(), strTime, formatTime).ToString();
							log.iPlatformID = Convert.ToInt32(DataArry[1]);
							log.iNodeID = Convert.ToInt32(DataArry[2]);
							log.iTypeID = Convert.ToInt32(DataArry[3]);
							log.iRuleID =  Convert.ToInt32(DataArry[4]);
							log.strRuleName = DataArry[5].ToString();
							log.strValue = DataArry[6].ToString();
							log.iStatus = Convert.ToInt32(DataArry[7]);
							string strAction = DataArry[8].ToString();
							string strParam = DataArry[9].ToString();
							log.strReserve1 = DataArry[10].ToString();
							log.strReserve2 = DataArry[11].ToString();

							string[] ActionArry = strAction.Split('&');
							log.iInterfaceAction = new int[ActionArry.Length];
							for(int i=0; i<ActionArry.Length; i++)
							{
								log.iInterfaceAction[i] = Convert.ToInt32(ActionArry[i]);
							}
							//log.iAction = Convert.ToInt32(DataArry[8]);


							string[] ParamArry = strParam.Split('&');
							log.strInActionParam = new string[ParamArry.Length];
							for(int i=0; i<ParamArry.Length; i++)
							{
								log.strInActionParam[i] = ParamArry[i];
							}
							
							//log.strActionParam = DataArry[9].ToString();
							m_queRecord.Enqueue(log);
						}
						else
						{
							DataArry = m_strLineData.Split('|');
							
							if(DataArry[0] != "ed" && DataArry[0] != "")
							{
								#region 获取流量数据
								
								SPFlowFile spFlowFile = new MonitorSystem.LogFileModule.SPFlowFile();
                                
								if(DataArry[0] == "sp")
								{
									
									spFlowFile.Code = DataArry[1].ToString();
									spFlowFile.Name = DataArry[2].ToString()+"-" + DataArry[3].ToString();
									spFlowFile.MOSUC = Convert.ToInt32(DataArry[4].ToString());
									spFlowFile.MOFAIL = Convert.ToInt32(DataArry[5].ToString());
									spFlowFile.MTSUC = Convert.ToInt32(DataArry[6].ToString());
									spFlowFile.MTFAIL = Convert.ToInt32(DataArry[7].ToString());
									spFlowFile.NOTIUSER = Convert.ToInt32(DataArry[8].ToString());
									spFlowFile.NOTISP = Convert.ToInt32(DataArry[9].ToString());
									spFlowFile.NOTIWEBUSER = Convert.ToInt32(DataArry[10].ToString());
									spFlowFile.NOTIWEBSP = Convert.ToInt32(DataArry[11].ToString());
								}
								else
								{
									
									spFlowFile.Code = DataArry[1].ToString();
									spFlowFile.Name = "";
									spFlowFile.MOSUC = Convert.ToInt32(DataArry[2].ToString());
									spFlowFile.MOFAIL = Convert.ToInt32(DataArry[3].ToString());
									spFlowFile.MTSUC = Convert.ToInt32(DataArry[4].ToString());
									spFlowFile.MTFAIL = Convert.ToInt32(DataArry[5].ToString());
									spFlowFile.ParentId = ParentId;
								}

								String tempFileName = Path.GetFileName(m_strFileName);
								spFlowFile.StartTime = new DateTime(Convert.ToInt32(tempFileName.Substring(3,4)),Convert.ToInt32(tempFileName.Substring(7,2)),
									Convert.ToInt32(tempFileName.Substring(9,2)),Convert.ToInt32(tempFileName.Substring(11,2)),Convert.ToInt32(tempFileName.Substring(13,2)),0);
								#endregion

								#region 调用存储过程插入流量表
								DataAccess dataAccess = new DataAccess(m_CfgFile.ConnectionStr);
								dataAccess.AddParameter("@ID",System.Data.SqlDbType.BigInt,System.Data.ParameterDirection.Output);
								dataAccess.AddParameter("@ParentID",spFlowFile.ParentId,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@Code",spFlowFile.Code,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@NAME",spFlowFile.Name,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@MTSuc",spFlowFile.MTSUC,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@MOSuc",spFlowFile.MOSUC,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@MTFail",spFlowFile.MTFAIL,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@MOFail",spFlowFile.MOFAIL,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@NotiUserNum",spFlowFile.NOTIUSER,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@NotiUserSp",spFlowFile.NOTISP,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@WebNotiUserNum",spFlowFile.NOTIWEBUSER,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@WebNotiSpNum",spFlowFile.NOTIWEBSP,System.Data.ParameterDirection.Input);
								dataAccess.AddParameter("@StartTime",spFlowFile.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),System.Data.ParameterDirection.Input);

								//执行存储过程
								dataAccess.Excute(System.Data.CommandType.StoredProcedure,"Insert_SpFlow");
								//得到返回值
								if(DataArry[0] == "sp")
								{
					
									ParentId = Convert.ToInt16(dataAccess.GetParamValue("@ID").ToString());
									
								}
								dataAccess.ClearParameters();
								#endregion
							}

						}
						
					}
				}
				catch(Exception e)
				{
					m_SysLog.WriteToSysLog("Center:监控日志文件读取错误,原因:{0}", e.Message+e.StackTrace);
				}
				finally
				{
					if(m_fstream!=null)
						m_fstream.Close();
					if(m_logReader!=null)
						m_logReader.Close();
				}

				try
				{
					//删除文件
					File.Delete(m_strFileName);
				}
				catch(Exception)
				{
				}

				try
				{
					Thread.Sleep(100);
				}
				catch(Exception)
				{
				}
			}
		}

		public void Startup()
		{ 				
			m_Thread = new Thread(new ThreadStart(this.Run));
			// Start the thread
			m_Pause = false;
			m_Exit = false;		
			m_Thread.Start();			
		}
		public void Join()
		{
			if(m_Thread != null)
			{
				m_Thread.Interrupt();
				m_Thread.Join();
			}
			else
			{
				return;
			}
		}
		public bool IsAlive()
		{
			if(m_Thread != null)
			{
				return m_Thread.IsAlive;
			}
			else
			{
				return false;
			}
		}
		public void Suspend()
		{
			if(m_Pause == false)
				m_Pause = true;
		}
		public void Resume()
		{
			if(m_Pause == true)
				m_Pause = false;		
		}
		public void SetExit()
		{
			if(m_Exit == false)
				m_Exit = true;		
		}
	}
}

⌨️ 快捷键说明

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