// ------- /// <summary> /// Supports the changing of file name, size, etc. /// </summary> public void ChangeFileProperties(FileParameterStruct newFileParameter) { if (!newFileParameter.ValidateConstructorParameters(false, out string problem)) { throw new Exception("LogFileWriter.ChangeProperties(): Invalide parameters '" + newFileParameter.ToString() + "'." + (problem == null ? "" : " The following problem occured: " + Environment.NewLine + problem)); } this.newFileParameter = newFileParameter; isNewFileParameter = true; }
/// <summary> /// Check if file parameters are valid /// </summary> public static bool ValidateConstructorParameters( string directoryPath, string fileName, long maxFileByteCount, int maxFileCount, out string problem) // { FileParameterStruct validateFileParameterStruct = new FileParameterStruct(directoryPath, fileName, "txt", maxFileByteCount, maxFileCount); return(validateFileParameterStruct.ValidateConstructorParameters(true, out problem)); }
/// <summary> /// Constructor /// /// Creates and opens/closes a file /// /// will throw an exception if file cannot be created /// </summary> public FileSizeManager(FileParameterStruct newParameter) { //ensure that parameters are valid. if (!newParameter.ValidateConstructorParameters(false, out string problem)) { throw new Exception("Cannot create '" + newParameter.ToString() + "'." + (problem == "" ? "" : " The following problem occured: " + Environment.NewLine + problem)); } //create directory if necessary and prepare filelist directoryInfo = Directory.CreateDirectory(newParameter.DirectoryPath); newParameter.UpdateDirectoryPath(directoryInfo.FullName); fileParameter = newParameter; fileList = new List <FileInfoNumberStruct>(); //Read fileinfos with file numbers into sorted fileList FileInfo[] fileInfos = directoryInfo.GetFiles(fileParameter.GetFileSearchPattern(), SearchOption.TopDirectoryOnly); currentFileNumber = 1; //TestFileName123.tst ==> 123 // TestFileName int startPos = fileParameter.FileName.Length; // . tst int extensionLength = 1 + fileParameter.FileExtension.Length; foreach (FileInfo fileInfo in fileInfos) { string fileName = fileInfo.Name; if (int.TryParse(fileName.Substring(startPos, fileName.Length - startPos - extensionLength), out int fileNumber)) { fileList.Add(new FileInfoNumberStruct(fileNumber, fileInfo)); if (fileNumber > currentFileNumber) { currentFileNumber = fileNumber; } } } fileList.Sort(); //make sure that the correct number of files exist, if (fileList.Count == 0) { createNewFile(); } else { enforceFileCountMax(); currentFileInfo = fileList[fileList.Count - 1].FileInfo; } }
/// <summary> /// Constructor LogFileWriter /// </summary> public LogFileWriter( FileParameterStruct fileParameter, Func <string>?getNewFileHeader = null, Func <string>?getNewDayHeader = null, int maxMessageQueueSize = maxMessageQueueSizeDefault, int logFileWriterTimerInitialDelay = 10, int logFileWriterTimerInterval = 10000) { this.getNewFileHeader = getNewFileHeader; this.getNewDayHeader = getNewDayHeader; this.maxMessageQueueSize = maxMessageQueueSize; fileSizeManager = new FileSizeManager(fileParameter); initialiseLogFileWriterTimer(logFileWriterTimerInitialDelay, logFileWriterTimerInterval); }
void initialiseNewParameter() { FileParameterStruct existingFileParameter = fileSizeManager.FileParameter; if (existingFileParameter.DirectoryPath == newFileParameter.DirectoryPath && existingFileParameter.FileName == newFileParameter.FileName && existingFileParameter.FileExtension == newFileParameter.FileExtension) // { //only max size or number of files has changed. Just change some FileSizeManager properties. fileSizeManager.ChangeProperties(newFileParameter.MaxFileByteCount, newFileParameter.MaxFileCount); } else { //file name or location has changed, a new FileSizeManager is needed. fileSizeManager = new FileSizeManager(newFileParameter); } isNewFileParameter = false; lastDate = DateTime.Now.Date; }