public void LogMgrShouldCorrectlyHandleInitializeToDifferentLogger()
        {
            var dir1Dto = new DirectoryDto(".\\logdir");

            DirectoryHelper.DeleteADirectory(dir1Dto);

            var fileStamp = DateHelper.NowYearMthDayHrsSecs();

            var defaultLogFileBaseName = ".\\logdir\\installLog.log";

            var mgr = new ConsoleCommandLogMgr(defaultLogFileBaseName,
                                                fileStamp,
                                                    string.Empty);

            Assert.IsTrue(mgr.DefaultConsoleLogPathFileBaseName == defaultLogFileBaseName);

            Assert.IsTrue(mgr.CurrentConsoleLogPathFileBaseName == defaultLogFileBaseName);

            var newLogFileBaseName = ".\\logdir\\install2Log.log";

            mgr.InitializeCmdConsoleLog(newLogFileBaseName);

            Assert.IsTrue(mgr.DefaultConsoleLogPathFileBaseName == defaultLogFileBaseName);

            Assert.IsTrue(mgr.CurrentConsoleLogPathFileBaseName == newLogFileBaseName);

            mgr.Dispose();

            DirectoryHelper.DeleteADirectory(dir1Dto);
        }
        public ExecuteConsoleCommand(ConsoleCommandDto cmdDto,
											ConsoleCommandLogMgr logMgr,
												ConsoleCommandLogMgr errLogMgr,
													WorkingDirectoryMgr wrkDirectoryMgr)
        {
            if (cmdDto == null)
            {
                var msg = "Console Command Dto is NULL!";
                var err = new FileOpsErrorMessageDto
                {
                    DirectoryPath = string.Empty,
                    ErrId = 1,
                    ErrorMessage = msg,
                    ErrSourceMethod = "Constructor",
                    LoggerLevel = LogLevel.FATAL
                };

                ErrorMgr.LoggingStatus = ErrorLoggingStatus.On;
                ErrorMgr.WriteErrorMsg(err);

                throw new ArgumentException(msg);

            }

            _executeCommand = cmdDto;
            _logMgr = logMgr;
            _errLogMgr = errLogMgr;
            _wrkDirectoryMgr = wrkDirectoryMgr;
        }
        public CommandExecutionMgr(JobsGroupDto jobsGroup,
								ConsoleExecutorDto consoleCmdDto)
        {
            _commandJobs = jobsGroup;

            _consoleExecutor = consoleCmdDto;

            _cmdLogMgr = new ConsoleCommandLogMgr(consoleCmdDto.DefaultCommandOutputLogFilePathName,
                                                    consoleCmdDto.CmdConsoleLogFileTimeStamp,
                                                        string.Empty);

            _errLogMgr = new ConsoleCommandLogMgr(consoleCmdDto.DefaultCommandOutputLogFilePathName,
                                                        consoleCmdDto.CmdConsoleLogFileTimeStamp,
                                                            consoleCmdDto.CmdConsoleLogFileErrorSuffix);

            _wrkDirMgr = new WorkingDirectoryMgr(_consoleExecutor.DefaultCommandExeDirectoryDto);
        }
        public void WriteLinesToTwoLoggers()
        {
            var dir1Dto = new DirectoryDto(".\\logdir");

            DirectoryHelper.DeleteADirectory(dir1Dto);

            var fileStamp = DateHelper.NowYearMthDayHrsSecs();

            var mgr = new ConsoleCommandLogMgr(".\\logdir\\installLog.log",
                                                fileStamp,
                                                    string.Empty);

            var currWrkDir = TestDirectories.GetTestExeDir();
            var subDir = "\\logdir\\installLog" + "_" + fileStamp + ".log";
            var expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir);

            mgr.InitializeCmdConsoleLog(string.Empty);

            var textOutput = "Hello World!";
            mgr.LogWriteLine(textOutput);
            var expectedText = textOutput + Environment.NewLine;

            mgr.LogFlushStreamWriter();

            StreamReader sReader = null;
            var sb = new StringBuilder();
            FileStream fStream = null;
            try
            {

                fStream = new FileStream(expectedResult.FileXinfo.FullName,
                    FileMode.Open,
                    FileAccess.Read,
                    FileShare.ReadWrite);

                sReader = new StreamReader(fStream);
                sb.Append(sReader.ReadToEnd());
            }
            catch (Exception ex)
            {
                // ReSharper disable once UnusedVariable
                var x = ex.Message;

            }
            finally
            {
                if (sReader != null)
                {
                    sReader.Close();
                    sReader.Dispose();
                    sReader = null;
                }

                if (fStream != null)
                {
                    fStream.Close();
                    fStream.Dispose();
                    fStream = null;
                }
            }
            var result = sb.ToString();

            Assert.IsTrue(result == expectedText);

            subDir = ".\\logdir\\installLog2" + "_" + fileStamp + ".log";
            var baseFile = ".\\logdir\\installLog2" +".log";
            expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir);

            mgr.InitializeCmdConsoleLog(baseFile);
            textOutput = "Hello2 World!";
            mgr.LogWriteLine(textOutput);
            expectedText = textOutput + Environment.NewLine;
            mgr.LogFlushStreamWriter();
            sb = new StringBuilder();

            try
            {
                fStream = new FileStream(expectedResult.FileXinfo.FullName,
                    FileMode.Open,
                    FileAccess.Read,
                    FileShare.ReadWrite);

                sReader = new StreamReader(fStream);
                sb.Append(sReader.ReadToEnd());
            }
            catch(Exception ex)
            {
                // ReSharper disable once UnusedVariable
                var y = ex.Message;
            }
            finally
            {
                if (sReader != null)
                {
                    sReader.Close();
                    sReader.Dispose();
                }

                if (fStream != null)
                {
                    fStream.Close();
                    fStream.Dispose();
                }

            }

            result = sb.ToString();
            mgr.Dispose();
            DirectoryHelper.DeleteADirectory(dir1Dto);

            Assert.IsTrue(result == expectedText);
        }
        public void LogMgrShouldInitializeCorrectly()
        {
            var fileStamp = DateHelper.NowYearMthDayHrsSecs();

            var mgr = new ConsoleCommandLogMgr(".\\logdir\\installLog.log",
                                                    fileStamp,
                                                        string.Empty);

            var currWrkDir = TestDirectories.GetTestExeDir();
            var subDir = "\\logdir\\installLog" + "_" + fileStamp + ".log";
            var expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir , subDir);
            var result = mgr.DefaultLogFileDto.FileXinfo.FullName;
            mgr.Dispose();

            Assert.IsTrue(result == expectedResult.FileXinfo.FullName);
        }
        public void WriteTextLinesToLogMgr()
        {
            var dir1Dto = new DirectoryDto(".\\logdir");

            DirectoryHelper.DeleteADirectory(dir1Dto);

            var fileStamp = DateHelper.NowYearMthDayHrsSecs();

            var mgr = new ConsoleCommandLogMgr(".\\logdir\\installLog.log",
                                                fileStamp,
                                                    string.Empty);

            var currWrkDir = TestDirectories.GetTestExeDir();
            var subDir = "\\logdir\\installLog" + "_" + fileStamp + ".log";
            var expectedResult = FileHelper.CombineDirSubDirWithFile(currWrkDir, subDir);

            mgr.InitializeCmdConsoleLog(string.Empty);

            var textOutput = "Hello World!";
            mgr.LogWriteLine(textOutput);
            var expectedText = textOutput+ Environment.NewLine;

            mgr.LogFlushStreamWriter();

            mgr.Dispose();

            StreamReader sReader = null;
            var sb = new StringBuilder();

            try
            {
                sReader = new StreamReader(expectedResult.FileXinfo.FullName);
                sb.Append(sReader.ReadToEnd());
                sReader.Close();
                sReader.Dispose();
            }
            catch
            {
                if (sReader != null)
                {
                    sReader.Close();
                    sReader.Dispose();
                }

                Assert.IsTrue(false);
            }
            var result = sb.ToString();

            DirectoryHelper.DeleteADirectory(dir1Dto);

            Assert.IsTrue(result == expectedText);
        }
        public bool ExecuteCommands()
        {
            bool result;
            int jobNo = 0;
            Console.WriteLine("CmdrX.exe: Starting Job Execution...");
            try
            {
                foreach (var job in _commandJobs.Jobs)
                {
                    jobNo++;
                    job.JobNumber = jobNo;
                    job.CommandStartTime = DateTime.Now;
                    LogUtil.WriteLogJobStartUpMessage(job, _consoleExecutor);
                    _cmdLogMgr.LogWriteStartJobHeader(job);
                    _errLogMgr.LogWriteStartJobHeader(job);
                    var exeCmd = new ExecuteConsoleCommand(job, _cmdLogMgr, _errLogMgr, _wrkDirMgr);
                    exeCmd.Execute();

                    _cmdLogMgr.LogWriteEndJobFooter(job);
                    _errLogMgr.LogWriteEndJobFooter(job);
                    Console.WriteLine($"Completed Job No. {jobNo,4:###0} Exit Code: {job.CommandExitCode} Job Name: {job.CommandDisplayName}" );

                    if (job.CommandExitCode > job.KillJobsRunOnExitCodeGreaterThan
                        || job.CommandExitCode < job.KillJobsRunOnExitCodeLessThan)
                    {
                        var msg = $"Job No. {job.JobNumber} Job Name: {job.CommandDisplayName}"
                            + Environment.NewLine
                            + $"Command Exit Code Is Out-Of-Bounds! Job Exit Code = {job.CommandExitCode}  "
                            + Environment.NewLine
                            + $"Maximum Exit Code = {job.KillJobsRunOnExitCodeGreaterThan}  "
                            + $"Mininimum Exit Code = {job.KillJobsRunOnExitCodeLessThan}  "
                            + Environment.NewLine
                            + "Terminating Job Group Command Execution!";

                        Environment.ExitCode = job.CommandExitCode;
                        var err = new FileOpsErrorMessageDto
                        {
                            JobName = job.CommandDisplayName,
                            DirectoryPath = string.Empty,
                            ErrId = 10,
                            ErrorMessage = msg,
                            ErrSourceMethod = "ExecuteCommands()",
                            FileName = string.Empty,
                            LoggerLevel = LogLevel.FATAL
                        };

                        _consoleExecutor.ApplicationExitStatus.OpsError =
                            ErrorMgr.FormatErrorDto(err);
                        _consoleExecutor.ApplicationExitStatus.IsFatalError = true;
                        ErrorMgr.LoggingStatus = ErrorLoggingStatus.On;
                        ErrorMgr.WriteErrorMsg(err);
                        LogUtil.WriteLogJobEndMessage(job, _consoleExecutor);
                        return false;
                    }

                    LogUtil.WriteLogJobEndMessage(job, _consoleExecutor);

                    _wrkDirMgr.ChangeBackToOriginalWorkingDirectory();
                }

                Environment.ExitCode = 0;
                _consoleExecutor.ApplicationExitStatus.IsSuccessfulCompletion = true;
                result = true;
            }
            catch (Exception ex)
            {
                var err = new FileOpsErrorMessageDto
                {
                    DirectoryPath = string.Empty,
                    ErrId = 20,
                    ErrorMessage = "Exception thrown while executing commands! ",
                    ErrSourceMethod = "ExecuteCommands()",
                    ErrException = ex,
                    FileName = string.Empty,
                    LoggerLevel = LogLevel.FATAL
                };

                ErrorMgr.LoggingStatus = ErrorLoggingStatus.On;
                ErrorMgr.WriteErrorMsg(err);
                _consoleExecutor.ApplicationExitStatus.IsExceptionThrown = true;
                _consoleExecutor.ApplicationExitStatus.OpsError = ErrorMgr.FormatErrorDto(err);

                if (Environment.ExitCode == 0)
                {
                    Environment.ExitCode = -8;
                }

                result = false;
            }
            finally
            {
                _wrkDirMgr.ChangeBackToOriginalWorkingDirectory();
                _wrkDirMgr.Dispose();
                _wrkDirMgr = null;

                _commandJobs.Dispose();
                _commandJobs = null;

                _cmdLogMgr.Dispose();
                _cmdLogMgr = null;

                _errLogMgr.Dispose();
                _errLogMgr = null;

            }

            return result;
        }