/// <summary> /// 日志压缩和回删 /// </summary> public void LogCompressionAndDelete() { string strProgramDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); DateTime dtCurrTime; DateTime strLastModifyTime; int iLogSaveTime = 0; //日志保存时长 默认30天 int iLogCompressTime = 0; //压缩多久前的日志 默认24小时 try { while (true) { //获得日志路径 string strLocalMachineDir = strProgramDataPath + "\\VoiceCyber\\UMP\\config"; DirectoryInfo dir = new DirectoryInfo(strLocalMachineDir); if (!dir.Exists) { //如果没有C:\ProgramData\VoiceCyber\UMP\config文件夹 说明程序有误 因为这个文件夹在服务启动时生成 UMPService00.WriteLog(LogMode.Error, "Can not find the path : " + strLocalMachineDir); break; } IniOperation ini = new IniOperation(strLocalMachineDir + "\\localmachine.ini"); string strLogPath = ini.IniReadValue("LocalMachine", "LogPath"); if (string.IsNullOrEmpty(strLogPath)) { //如果没有配置日志路径 暂停5分钟 UMPService00.WriteLog(LogMode.Error, "No configuration log path , pause 5 minutes"); Thread.Sleep(5 * 60 * 1000); continue; } //判断日志路径是否存在 dir = new DirectoryInfo(strLogPath); if (!dir.Exists) { UMPService00.WriteLog(LogMode.Error, "Can not find the log file path : " + strLogPath + ", pause 5 minutes"); Thread.Sleep(5 * 60 * 1000); continue; } GetLogParam(ref iLogSaveTime, ref iLogCompressTime); // UMPService00.IEventLog.WriteEntry("iLogSaveTime = " + iLogSaveTime + " ; iLogCompressTime = " + iLogCompressTime, EventLogEntryType.Warning); //获得当前时间 dtCurrTime = DateTime.Now; LogFileOperate(dir, dtCurrTime, iLogSaveTime, iLogCompressTime); UMPService00.WriteLog("ok ,sleep 3 mins"); Thread.Sleep(3 * 60 * 1000); } } catch (Exception ex) { UMPService00.WriteLog(LogMode.Error, "LogCompressionAndDelete() error : " + ex.Message); } }
/// <summary> /// 写入日志路径到LocalMachine.ini中 /// </summary> public static bool WriteLogPath(string strPath) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(strPath); XMLOperator xmlOperator = new XMLOperator(xmldoc); XmlNode LocalMachineNode = xmlOperator.SelectNode("Configurations/Configuration/LocalMachine", ""); string strLogPath = xmlOperator.SelectAttrib(LocalMachineNode, "LogPath"); string strProgramDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\VoiceCyber\\UMP\\config"; string strLocalMachineIni = strProgramDataPath + "\\localmachine.ini"; IniOperation ini = new IniOperation(strLocalMachineIni); ini.IniWriteValue("LocalMachine", "LogPath", strLogPath); // UMPService00.IEventLog.WriteEntry("LogPath = " + strLogPath, EventLogEntryType.Warning); return(true); }
/// <summary> /// 修改或创建LocalMachine.ini 获得本机的IP协议 写入多播的IP和端口、MachineID /// </summary> private void CreatelLocalMachineIni() { string strProgramDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\VoiceCyber\\UMP\\config"; DirectoryInfo dir = new DirectoryInfo(strProgramDataPath); if (!dir.Exists) { dir.Create(); } string strIniPath = strProgramDataPath + "\\localmachine.ini"; FileInfo fi = new FileInfo(strIniPath); if (!fi.Exists) { FileStream fs = fi.Create(); fs.Close(); } IniOperation ini = new IniOperation(strIniPath); string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); if (ipEntry.AddressList.Count() > 0) { foreach (IPAddress adress in ipEntry.AddressList) { if (adress.AddressFamily == AddressFamily.InterNetwork) { ini.IniWriteValue("LocalMachine", "SubscribeAddress", "224.0.2.26,3789"); break; } //else if (adress.AddressFamily == AddressFamily.InterNetworkV6) //{ // ini.IniWriteValue("LocalMachine", "SubscribeAddress", "ff01::0226,3789"); // break; //} continue; } } string strMachineID = ini.IniReadValue("LocalMachine", "MachineID"); if (string.IsNullOrEmpty(strMachineID)) { ini.IniWriteValue("LocalMachine", "MachineID", Guid.NewGuid().ToString()); } }
/// <summary> /// 发送消息 /// </summary> /// <param name="strMessage">xml文件路径</param> public static bool SendBroadcastMessage(string strMessage) { string strProgramDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\VoiceCyber\\UMP\\config"; string strLocalMachineIni = strProgramDataPath + "\\localmachine.ini"; IniOperation ini = new IniOperation(strLocalMachineIni); string strHostAndPort = ini.IniReadValue("LocalMachine", "SubscribeAddress"); //IP端口字符串不正确 if (string.IsNullOrEmpty(strHostAndPort) || !strHostAndPort.Contains(',')) { // UMPService00.IEventLog.WriteEntry("localmachine.ini SubscribeAddress value is error", EventLogEntryType.Error); UMPService00.WriteLog(LogMode.Error, "localmachine.ini SubscribeAddress value is error"); return(false); } string strHost = strHostAndPort.Substring(0, strHostAndPort.IndexOf(',')); int iPort = int.Parse(strHostAndPort.Substring(strHostAndPort.IndexOf(',') + 1)); string strMachineID = ini.IniReadValue("LocalMachine", "MachineID"); IPAddress address = IPAddress.Parse(strHost); // IPAddress address = IPAddress.Parse("fe80::c4c2:9114:91e8:fbad%12"); Socket socket = null; if (address.AddressFamily == AddressFamily.InterNetwork) { //UMPService00.IEventLog.WriteEntry("IPV4", EventLogEntryType.Warning); socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } else if (address.AddressFamily == AddressFamily.InterNetworkV6) { // UMPService00.IEventLog.WriteEntry("IPV6", EventLogEntryType.Warning); socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); } // UMPService00.IEventLog.WriteEntry("strHost = " + strHost + "\r\niPort = " + iPort.ToString(), EventLogEntryType.Warning); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(address)); //SocketOptionName.MulticastTimeToLive IP多路广播生存时间。 socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPEndPoint ipep = new IPEndPoint(address, iPort); socket.Connect(ipep); SendDgramMsg(strMessage, strMachineID, socket); return(true); }