/// <summary> /// 获取当前服务安装的路径 /// </summary> /// <returns></returns> public string GetCurrentBaseDirectory() { string LStrReturn = string.Empty; try { LStrReturn = AppDomain.CurrentDomain.BaseDirectory; string[] LStrDerectoryArray = LStrReturn.Split('\\'); LStrReturn = string.Empty; foreach (var LStrDirectorySingle in LStrDerectoryArray) { if (LStrDirectorySingle.ToLower() == "winservices") { break; } LStrReturn += LStrDirectorySingle + "\\"; //FileLog.WriteInfo("GetCurrentBaseDirectory()", "Read AppDomain.CurrentDomain.BaseDirectory : " + LStrReturn); } } catch (Exception ex) { LStrReturn = string.Empty; FileLog.WriteError("GetCurrentBaseDirectory()", "Read AppDomain.CurrentDomain.BaseDirectory Failed \n" + ex.ToString()); } return(LStrReturn); }
private void WaitForSocketClientConnect() { bool LBoolReturn = true; string LStrReturn = string.Empty; FileLog.WriteInfo("WaitForSocketClientConnect()", ""); try { while (IBoolCanContinue) { if (!ITcpListener.Pending()) { Thread.Sleep(200); continue; } TcpClient LTcpClient = ITcpListener.AcceptTcpClient(); if (!IBoolCanContinue) { return; } IBoolIsBusing = true; ClientOperations LClientOperations = new ClientOperations(LTcpClient); Thread LClientThread = new Thread(new ThreadStart(LClientOperations.ClientMessageOperation)); FileLog.WriteInfo("WaitForSocketClientConnect()", "1"); AddConnectedClientAndThread(LClientOperations, LClientThread, ref LBoolReturn, ref LStrReturn); if (LBoolReturn) { LClientThread.Start(); } else { FileLog.WriteError("WaitForSocketClientConnect()", LStrReturn); } Thread.Sleep(100); IBoolIsBusing = false; } } catch (Exception ex) { IBoolCanContinue = false; FileLog.WriteInfo("WaitForSocketClientConnect()", "Failed\n" + ex.ToString()); } }
private void CloseAllClientThread() { int LIntAllConnected = 0; try { LIntAllConnected = GlobalListConnectedClient.Count - 1; while (LIntAllConnected >= 0) { GlobalListConnectedClient[LIntAllConnected].StopThisClientThread(); LIntAllConnected -= 1; } } catch (Exception ex) { FileLog.WriteError("CloseAllClientThread()", ex.ToString()); } }
/// <summary> /// 压缩到期的log文件 并删掉压缩完成对log文件 /// </summary> private void CompressFile(DirectoryInfo dir, int iLogCompressTime, DateTime dtCurrTime) { List <FileInfo> lstFiles = dir.GetFiles("*.log").ToList(); ZipOutputStream zipOutStream = null; string strZipFileName = string.Empty; TimeSpan tspan; foreach (FileInfo file in lstFiles) { try { tspan = dtCurrTime - file.LastWriteTime; if (tspan.TotalHours < iLogCompressTime) { continue; } strZipFileName = file.Name + ".zip"; zipOutStream = new ZipOutputStream(File.Create(System.IO.Path.Combine(file.DirectoryName, strZipFileName))); zipOutStream.SetLevel(9); byte[] LByteBuffer = new byte[1024]; ZipEntry LZipEntry = new ZipEntry(file.Name); LZipEntry.DateTime = DateTime.UtcNow; zipOutStream.PutNextEntry(LZipEntry); using (FileStream LFileStream = File.OpenRead(file.FullName)) { int LIntSourceBytes; do { LIntSourceBytes = LFileStream.Read(LByteBuffer, 0, LByteBuffer.Length); zipOutStream.Write(LByteBuffer, 0, LIntSourceBytes); } while (LIntSourceBytes > 0); } zipOutStream.Finish(); zipOutStream.Close(); file.Delete(); Thread.Sleep(50); } catch (Exception ex) { FileLog.WriteError("CompressFile()", " error : " + ex.Message); } } }
/// <summary> /// 日志压缩和回删 /// </summary> public void LogCompressionAndDelete() { string strProgramDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); DateTime dtCurrTime; int iLogSaveTime = 0; //日志保存时长 默认30天 int iLogCompressTime = 0; //压缩多久前的日志 默认24小时 //得到配置文件里的压缩时间以及回删日志时间 string strCompressLogHour = ConfigurationManager.AppSettings["CompressLogHour"] != null ? ConfigurationManager.AppSettings["CompressLogHour"] : "6"; string strDeleteLogDay = ConfigurationManager.AppSettings["CompressLogDay"] != null ? ConfigurationManager.AppSettings["CompressLogDay"] : "15"; try { while (true) { //获得日志路径 string strLocalMachineDir = strProgramDataPath + "\\UMP\\UMPService06\\Log"; DirectoryInfo dir = new DirectoryInfo(strLocalMachineDir); if (!dir.Exists) { Thread.Sleep(5 * 60 * 1000); break; } //获得当前时间 dtCurrTime = DateTime.Now; iLogCompressTime = IntParse(strCompressLogHour, 6); iLogSaveTime = IntParse(strDeleteLogDay, 15); LogFileOperate(dir, dtCurrTime, iLogSaveTime, iLogCompressTime); Thread.Sleep(3 * 60 * 1000); } } catch (Exception ex) { FileLog.WriteError("LogCompressionAndDelete()", " error : " + ex.Message); } }
private void AddConnectedClientAndThread(ClientOperations AClientOperation, Thread AClientThread, ref bool ABoolReturn, ref string AStrReturn) { try { ABoolReturn = true; AStrReturn = string.Empty; lock (ILockObjectCheckThread) { GlobalListConnectedClient.Add(AClientOperation); GlobalListClientThread.Add(AClientThread); FileLog.WriteInfo("AddConnectedClientAndThread()", "AddConnectedClientAndThread() GlobalListClientThread.Count = " + GlobalListClientThread.Count.ToString() + " GlobalListConnectedClient.Count = " + GlobalListConnectedClient.Count.ToString()); } } catch (Exception ex) { ABoolReturn = false; AStrReturn = "AddConnectedClientAndThread()\n" + ex.Message; FileLog.WriteError("AddConnectedClientAndThread()", ex.Message.ToString()); } }
protected override void OnStop() { try { IBoolCanContinue = false; while (IBoolIsBusing) { Thread.Sleep(100); } if (ITcpListener != null) { ITcpListener.Stop(); ITcpListener = null; } CloseAllClientThread(); FileLog.WriteInfo("Service Stopped At :", DateTime.Now.ToString("G")); if (ThreadCheckThreadIsAlive != null) { ThreadCheckThreadIsAlive.Abort(); ThreadCheckThreadIsAlive = null; } if (IThreadLogOperation != null) { try { IThreadLogOperation.Abort(); IThreadLogOperation = null; } catch (System.Exception e) { } } } catch (Exception ex) { FileLog.WriteError("Service Stopped Error At :", ex.ToString()); } }
/// <summary> /// 获取数据路类型及数据库连接串 /// </summary> /// <returns>string.Empty成功,Error01未进行数据库配置,否则失败</returns> /// #region 获取数据库连接信息 private void GetDatabaseConnectionProfile() { string LStrXmlFileName = string.Empty; string LStrVerificationCode = string.Empty; string LStrAttributesData = string.Empty; //0:数据库服务器;1:端口;2:数据库名或服务名;3:登录用户;4:登录密码;5:其他参数 List <string> LListStrDBProfile = new List <string>(); try { IIntDBType = 0; IStrDBConnectProfile = string.Empty; LStrVerificationCode = CreateVerificationCode(EncryptionAndDecryption.UMPKeyAndIVType.M104); LStrXmlFileName = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); //LStrXmlFileName =System.AppDomain.CurrentDomain.BaseDirectory;//测试用 LStrXmlFileName = System.IO.Path.Combine(LStrXmlFileName, @"UMP.Server\Args01.UMP.xml"); XmlDocument LXmlDocArgs01 = new XmlDocument(); LXmlDocArgs01.Load(LStrXmlFileName); XmlNodeList LXmlNodeListDatabase = LXmlDocArgs01.SelectSingleNode("DatabaseParameters").ChildNodes; #region 读取数据库连接参数 foreach (XmlNode LXmlNodeSingleDatabase in LXmlNodeListDatabase) { LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P03"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); if (LStrAttributesData != "1") { continue; } //数据库类型 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P02"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); IIntDBType = int.Parse(LStrAttributesData); //数据库服务器名或IP地址 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P04"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); //数据库服务端口 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P05"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); //数据库名或Service Name LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P06"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); //登录用户 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P07"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); //登录密码 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P08"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); //其他参数 LStrAttributesData = LXmlNodeSingleDatabase.Attributes["P09"].Value; LStrAttributesData = EncryptionAndDecryption.EncryptDecryptString(LStrAttributesData, LStrVerificationCode, EncryptionAndDecryption.UMPKeyAndIVType.M104); LListStrDBProfile.Add(LStrAttributesData); break; } #endregion #region 创建数据库连接字符串 string LStrDBConnectProfile = string.Empty; if (IIntDBType == 2) { IStrDBConnectProfile = string.Format("Data Source={0},{1};Initial Catalog={2};User Id={3};Password={4}", LListStrDBProfile[0], LListStrDBProfile[1], LListStrDBProfile[2], LListStrDBProfile[3], LListStrDBProfile[4]); LStrDBConnectProfile = string.Format("Data Source={0},{1};Initial Catalog={2};User Id={3};Password={4}", LListStrDBProfile[0], LListStrDBProfile[1], LListStrDBProfile[2], LListStrDBProfile[3], "******"); LStrDBConnectProfile = "DataBase Type : MS SQL Server\n" + LStrDBConnectProfile; } if (IIntDBType == 3) { IStrDBConnectProfile = string.Format("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0}) (PORT={1})))(CONNECT_DATA=(SERVICE_NAME= {2})));User Id={3}; Password={4}", LListStrDBProfile[0], LListStrDBProfile[1], LListStrDBProfile[2], LListStrDBProfile[3], LListStrDBProfile[4]); LStrDBConnectProfile = string.Format("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0}) (PORT={1})))(CONNECT_DATA=(SERVICE_NAME= {2})));User Id={3}; Password={4}", LListStrDBProfile[0], LListStrDBProfile[1], LListStrDBProfile[2], LListStrDBProfile[3], "******"); LStrDBConnectProfile = "DataBase Type : Oracle\n" + LStrDBConnectProfile; } #endregion FileLog.WriteInfo("GetDatabaseConnectionProfile() ", "LStrDBConnectProfile=" + LStrDBConnectProfile); } catch (Exception ex) { IIntDBType = 0; IStrDBConnectProfile = string.Empty; FileLog.WriteError("GetDatabaseConnectionProfile()", "Error :" + ex.ToString()); } }