예제 #1
0
파일: Program.cs 프로젝트: ragnaroks/Wind
 /// <summary>
 /// 初始化日志模块
 /// </summary>
 /// <returns>是否成功</returns>
 private static Boolean InitializeLoggerModule()
 {
     if (!Directory.Exists(AppEnvironment.LogsDirectory))
     {
         try {
             _ = Directory.CreateDirectory(AppEnvironment.LogsDirectory);
         } catch (Exception exception) {
             Helpers.LoggerModuleHelper.TryLog(
                 "Program.InitializeLoggerModule[Error]", $"创建日志目录异常,{exception.Message}\n异常堆栈: {exception.StackTrace}");
             return(false);
         }
     }
     return(LoggerModule.Setup(AppEnvironment.LogsDirectory, 1000));
 }
예제 #2
0
파일: Program.cs 프로젝트: ragnaroks/Wind
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        private static Boolean Initialize()
        {
            //互斥
            AppMutex = new Mutex(true, "WindCommandLineController", out Boolean mutex);
            if (!mutex)
            {
                LoggerModuleHelper.TryLog("Program.Initialize[Error]", "已存在实例");
                return(false);
            }
            //读取配置
            if (!Directory.Exists(AppEnvironment.BaseDirectory))
            {
                return(false);
            }
            String appSettingsFilePath = String.Concat(AppEnvironment.DataDirectory, Path.DirectorySeparatorChar, "AppSettings.json");

            if (!File.Exists(appSettingsFilePath))
            {
                return(false);
            }
            Entities.Common.AppSettings appSettings;
            FileStream fs = null;

            try {
                fs = File.Open(appSettingsFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                if (fs.Length < 1 || fs.Length > 4096)
                {
                    return(false);
                }
                Span <Byte> bufferSpan = new Span <Byte>(new Byte[fs.Length]);
                fs.Read(bufferSpan);
                fs.Dispose();
                appSettings = JsonSerializer.Deserialize <Entities.Common.AppSettings>(bufferSpan);
            }catch (Exception exception) {
                LoggerModuleHelper.TryLog("Program.Initialize[Error]", $"读取应用程序配置文件异常,{exception.Message}\n异常堆栈: {exception.StackTrace}");
                return(false);
            }finally{
                fs?.Dispose();
            }
            if (appSettings == null || String.IsNullOrWhiteSpace(appSettings.RemoteControlAddress) || String.IsNullOrWhiteSpace(appSettings.RemoteControlKey) || appSettings.RemoteControlPort < 1024 || appSettings.RemoteControlPort > Int16.MaxValue)
            {
                return(false);
            }
            Regex regex = new Regex(@"^[0-9\.]{7,15}$", RegexOptions.Compiled);

            if (appSettings.RemoteControlAddress != "localhost" && !regex.IsMatch(appSettings.RemoteControlAddress))
            {
                return(false);
            }
            Regex regex2 = new Regex(@"^\S{32,4096}$", RegexOptions.Compiled);

            if (!regex2.IsMatch(appSettings.RemoteControlKey))
            {
                return(false);
            }
            AppSettings.RemoteControlAddress = appSettings.RemoteControlAddress;
            AppSettings.RemoteControlPort    = appSettings.RemoteControlPort;
            AppSettings.RemoteControlKey     = appSettings.RemoteControlKey;
            //初始化日志模块
            if (!LoggerModule.Setup(AppEnvironment.LogsDirectory, 1000))
            {
                LoggerModuleHelper.TryLog("Program.Initialize[Error]", "初始化日志模块失败");
                return(false);
            }
            //初始化控制模块
            if (!RemoteControlModule.Setup(AppSettings.RemoteControlAddress, AppSettings.RemoteControlPort, AppSettings.RemoteControlKey))
            {
                LoggerModuleHelper.TryLog("Program.Initialize[Error]", "初始化远程控制模块失败");
                return(false);
            }
            //
            Console.CancelKeyPress += ConsoleCancelKeyPress;
            return(true);
        }