public static bool CreateAFile(FileDto fileDto) { if (!IsFileDtoValid(fileDto)) { return false; } fileDto.FileXinfo.Refresh(); if (fileDto.FileXinfo.Exists) { return true; } try { fileDto.FileXinfo.Create(); } catch { return false; } return true; }
public StreamWriterDto(FileDto swFile, Encoding streamWriterEncoding) { if (!FileHelper.IsFileDtoValid(swFile)) { var ex = new Exception("Invalid FileDto Passed to StreamWriter!"); var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 2, ErrorMessage = ex.Message, ErrSourceMethod = "SetStreamWriter()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } _streamWriterEncoding = streamWriterEncoding; CreateStreamWriter(swFile.FileXinfo.FullName); }
public void ConsoleCommandShouldCorrectlyProcessWithNoCommandExecutor() { var mainTestDir = TestDirectories.GetMainLibLoadTestDir(); var xmlFileDto = new FileDto(mainTestDir, ".\\XmlCmdFiles\\CmdFile002.xml"); if (!FileHelper.IsFileDtoValid(xmlFileDto) || !xmlFileDto.FileXinfo.Exists) { Assert.IsTrue(false); } var cmdExeDto = TestDirectories.GetConsoleExecutorDto(); cmdExeDto.XmlCmdFileDto = xmlFileDto; var builder = new XmlParameterBuilder(cmdExeDto); var jobGroup = builder.BuildParmsFromXml(); var job = jobGroup.Jobs[0]; Assert.IsNotNull(job); var expectedProcFile = "npm"; var expectedProcArgs = "install --save-dev gulp-jshint gulp-jscs jshint-stylish"; Assert.IsTrue(job.ProcFileNameCommand == expectedProcFile); Assert.IsTrue(job.ProcFileArguments == expectedProcArgs); }
public ApplicationLogger( string defaultApplicationLogPathFileName, string logFileTimeStamp) { _logfileTimeStamp = StringHelper.TrimStringEnds(logFileTimeStamp); _defautlLogFileDto = ExtractLogFileDto(defaultApplicationLogPathFileName, _logfileTimeStamp); _defaultApplicationLogPathFileBaseName = defaultApplicationLogPathFileName; if (!FileHelper.IsFileDtoValid(_defautlLogFileDto)) { var msg = "Default Command Console Log File is INVALID!"; var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 1, ErrorMessage = msg, ErrSourceMethod = "Constructor()", FileName = defaultApplicationLogPathFileName, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw new Exception(msg); } try { FileHelper.DeleteAFile(_defautlLogFileDto); _currentApplicationLogPathFileBaseName = defaultApplicationLogPathFileName; _currentLogfileDto = new FileDto(_defautlLogFileDto.FileXinfo.FullName); } catch (Exception ex) { var msg = "Consold Command Log Setup Failed! " + ex.Message; var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 3, ErrorMessage = msg, ErrSourceMethod = "Constructor()", FileName = defaultApplicationLogPathFileName, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw; } }
public void Test002CreateFileDtoUsingFileNameOnly() { var testStr = @"SomefileName.txt"; var expectedDir = TestDirectories.GetTestExeDir(); var expectedResult = Path.Combine(expectedDir.DirInfo.FullName, testStr); var fileDto = new FileDto(testStr); Assert.IsTrue(fileDto.FileXinfo.FullName == expectedResult); }
public void Test001CreateFileDtoUsingFileNameString() { var testStr = @"D:\Level1\Level2\SomefileName.txt"; var fileDto = new FileDto(testStr); Assert.IsTrue(fileDto.FileXinfo.FullName == testStr); }
public void Test003CreateFileDtoWithNoExt() { var testStr = @"D:\Level1\Level2"; var expectedResult = "Level2"; var expectedDir = @"D:\Level1"; var fileDto = new FileDto(testStr); Assert.IsTrue(fileDto.FileXinfo.Name == expectedResult); Assert.IsTrue(fileDto.DirDto.DirInfo.FullName == expectedDir); }
public void Test005CreateFileDtoFromFileAndDirectoryCombination() { var testStr = @"D:\Level1\Level2"; var testDirDto = new DirectoryDto(testStr); var fileComponent = @"SomefileName.txt"; var expectedResult = @"D:\Level1\Level2\SomefileName.txt"; var fileDto = new FileDto(testDirDto, fileComponent); Assert.IsTrue(FileHelper.IsFileDtoValid(fileDto)); Assert.IsTrue(fileDto.FileXinfo.FullName == expectedResult); }
private bool DeleteOldLogFiles(string[] logFiles) { if (logFiles == null || logFiles.Length < 1) { return true; } TimeSpan dif = new TimeSpan(_logRetentionInDays, 0, 0, 0); DateTime threshold = DateTime.Now.Subtract(dif); foreach (var logFile in logFiles) { try { var fileDto = new FileDto(logFile); if (fileDto.FileXinfo.CreationTime < threshold) { FileHelper.DeleteAFile(fileDto); } } catch (Exception ex) { var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 79, ErrorMessage = "Exception Thrown While Deleting Log File!", ErrSourceMethod = "PurgeOldLogs", ErrException = ex, FileName = logFile, LoggerLevel = LogLevel.INFO }; ErrorMgr.WriteErrorMsg(err); } } return true; }
public static bool CreateDirectoryAndFile(FileDto fileDto) { if (!IsFileDtoValid(fileDto)) { return false; } fileDto.DirDto.DirInfo.Refresh(); if (fileDto.FileXinfo.Exists) { return true; } if (!fileDto.DirDto.DirInfo.Exists) { try { fileDto.DirDto.DirInfo.Create(); fileDto.DirDto.DirInfo.Refresh(); if (!fileDto.DirDto.DirInfo.Exists) { return false; } } catch { return false; } } return CreateAFile(fileDto); }
public StreamWriterDto(FileDto swFile) : this(swFile, Encoding.UTF8) { }
private void SetLogFileDto() { LogPathFileNameDto?.Dispose(); var fName = LogFileNameOnly + "_" + LogFileTimeStamp + "." + LogFileExtensionWithNoLeadingDot; var fileDto = new FileDto(LogDirectoryDto, fName); if (!FileHelper.IsFileDtoValid(fileDto)) { var msg = "Application Log Directory Dto Invalid!"; var ex = new ArgumentException(msg); var err = new FileOpsErrorMessageDto { ErrId = 35, ErrorMessage = "Application Log Directory Dto Invalid!", ErrSourceMethod = "CreateApplicaitonLogDirectory()", ErrException = ex, FileName = fName, DirectoryPath = LogDirectoryDto?.DirInfo?.FullName, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } LogPathFileNameDto = fileDto; }
public static bool CreateDirectoryIfNecessary(FileDto fileDto) { if (!FileHelper.IsFileDtoValid(fileDto)) { return false; } return CreateDirectoryIfNecessary(fileDto.DirDto); }
public static bool DeleteAFile(FileDto fileDto) { if (fileDto?.FileXinfo == null) { return false; } if (!fileDto.FileXinfo.Exists) { return true; } try { fileDto.FileXinfo.Delete(); } catch { return false; } return true; }
protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!_disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (_swDto != null) { _swDto.GetStreamWriter().Flush(); _swDto.Close(); _swDto = null; } if (_defautlLogFileDto != null) { _defautlLogFileDto.Dispose(); _defautlLogFileDto = null; } if (_currentLogfileDto != null) { _currentLogfileDto.Dispose(); _currentLogfileDto = null; } } // Note disposing has been done. _disposed = true; } }
public static bool DeleteFileWithZeroBytes(FileDto fileDto) { if (!IsFileDtoValid(fileDto)) { return false; } return fileDto.FileXinfo.Length != 0 || DeleteAFile(fileDto); }
protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!_disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. if (XmlCmdFileDto != null) { XmlCmdFileDto.Dispose(); XmlCmdFileDto = null; } if (DefaultCommandExeDirectoryDto!=null) { DefaultCommandExeDirectoryDto.Dispose(); DefaultCommandExeDirectoryDto = null; } } // Note disposing has been done. _disposed = true; } }
public void CreateNewStreamWriter(FileDto fileDto) { CloseStreamWriter(); _swDto = new StreamWriterDto(_currentLogfileDto); }
public static FileDto GetXmlCmdTest002FileDto() { var fileName = "\\XmlCmdFiles\\Test002.xml"; var testDir = GetMainLibLoadTestDir(); var result = new FileDto(testDir.DirInfo.FullName + fileName); if (!FileHelper.IsFileDtoValid(result) || !result.FileXinfo.Exists) { throw new Exception("Invalid FileDto created for XmlCmdFile: " + fileName); } return result; }
public static FileDto GetXmlCmdFileDto() { var fileName = ConfigurationManager.AppSettings["DefaultXmlCmdFile"]; var testDir = GetMainLibLoadTestDir(); var result = new FileDto(testDir.DirInfo.FullName + "\\" + fileName); if (!FileHelper.IsFileDtoValid(result)) { throw new Exception("Invalid FileDto created for XmlCmdFile: " + fileName); } return result; }
public static bool DoesFileExist(FileDto targetFileDto) { if (targetFileDto?.FileXinfo == null) { return false; } return targetFileDto.FileXinfo.Exists; }
public void Close() { if (_swWriter != null) { _swWriter.Flush(); _swWriter.Close(); _swWriter.Dispose(); _swWriter = null; } if (_fStream != null) { _fStream.Close(); _fStream.Dispose(); _fStream = null; } if (StreamWriterFile != null) { StreamWriterFile.Dispose(); StreamWriterFile = null; } }
public static bool IsFileDtoValid(FileDto fileDto) { if (fileDto?.FileXinfo == null || fileDto.DirDto == null) { return false; } if (string.IsNullOrWhiteSpace(fileDto.FileXinfo.FullName) || fileDto.FileXinfo.FullName.Length < 4) { return false; } if (!DirectoryHelper.IsDirectoryDtoValid(fileDto.DirDto)) { return false; } return true; }
public bool CreateStreamWriter(string swFileName) { Close(); StreamWriterFile = new FileDto(swFileName); if (!FileHelper.IsFileDtoValid(StreamWriterFile)) { var ex = new Exception("Invalid FileDto Passed to StreamWriter!"); var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 30, ErrorMessage = ex.Message, ErrSourceMethod = "CreateStreamWriter()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } try { StreamWriterFile.DirDto.DirInfo.Refresh(); if (!StreamWriterFile.DirDto.DirInfo.Exists) { if (!DirectoryHelper.CreateDirectoryIfNecessary(StreamWriterFile)) { var ex = new Exception("Failed to create directory for StreamWriter!"); var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 30, ErrorMessage = ex.Message, ErrSourceMethod = "CreateStreamWriter()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw ex; } } _fStream = new FileStream(StreamWriterFile.FileXinfo.FullName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); _swWriter = new StreamWriter(_fStream,_streamWriterEncoding); } catch(Exception ex) { var err = new FileOpsErrorMessageDto { DirectoryPath = string.Empty, ErrId = 32, ErrorMessage = "Failed To Create StreamWriter! " + ex.Message, ErrSourceMethod = "SetStreamWriter()", ErrException = ex, FileName = string.Empty, LoggerLevel = LogLevel.FATAL }; ErrorMgr.LoggingStatus = ErrorLoggingStatus.On; ErrorMgr.WriteErrorMsg(err); throw; } return true; }
public void LoggerShouldInitializeCorrectly() { var appLogDir = ".//installDir"; var logDirDto = new DirectoryDto(appLogDir); if (logDirDto.DirInfo.Exists) { DirectoryHelper.DeleteADirectory(logDirDto); } var appLogFileNameOnly = "LibLoadTests"; var appLogFileExensionWithoutLeadingDot = "log"; var appLogFileTimeStamp = DateHelper.NowYearMthDayHrsSecs(); var appLogMgr = new ApplicationLogMgr(appLogDir, appLogFileNameOnly, appLogFileExensionWithoutLeadingDot, appLogFileTimeStamp); var logger = appLogMgr.ConfigureLogger(); Assert.IsTrue(appLogMgr.LogPathFileNameDto.FileXinfo.Exists); var logFileName = appLogMgr.LogPathFileNameDto.FileXinfo.FullName; LogUtil.ConfigureLogger(logger); LogUtil.WriteLog(LogLevel.INFO, "Test9758: Originated from Test Method: LoggerShouldInitializeCorrectly()"); LogUtil.Dispose(); appLogMgr.Dispose(); FileStream fs = new FileStream(logFileName,FileMode.Open,FileAccess.Read,FileShare.Read); StreamReader fr = new StreamReader(fs); var sb = new StringBuilder(); sb.Append(fr.ReadToEnd()); fs.Flush(); fs.Close(); fr.Close(); fs.Dispose(); fr.Dispose(); var s = sb.ToString(); var result = s.IndexOf("Test9758", StringComparison.Ordinal); var logFileDto = new FileDto(logFileName); DirectoryHelper.DeleteADirectory(logDirDto); logFileDto.FileXinfo.Refresh(); Assert.IsFalse(logFileDto.FileXinfo.Exists); logFileDto.Dispose(); logDirDto.Dispose(); Assert.IsTrue(result > -1); }