Skip to content

Commit

Permalink
Minor code formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyMF committed Jun 5, 2024
1 parent beee8e0 commit 4313f9c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 66 deletions.
24 changes: 11 additions & 13 deletions src/NReco.Logging.File/FileLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace NReco.Logging.File {
/// <summary>
/// Generic file logger options.
/// </summary>
public class FileLoggerOptions
{
public class FileLoggerOptions {
/// <summary>
/// Append to existing log files or override them.
/// </summary>
Expand Down Expand Up @@ -102,20 +101,19 @@ public class FileLoggerOptions
/// Determines the naming convention and order of rolling files.
/// </summary>
public FileRollingConvention RollingFilesConvention { get; set; } = FileRollingConvention.Ascending;

/// <summary>
/// Holds the different file rolling convention, the default option being Ascending.
/// </summary>
public enum FileRollingConvention
{
/// <summary>
/// (Default) New files will get an ascending rolling index, files get rolled after max 0-1-2-3-0-1-2-3.
/// </summary>
Ascending,
/// <summary>
/// New files will get an ascending rolling index, but the latest file is always the file without index. More performant alt for descending rolling. 0-1-2-3-1-2-3
/// </summary>
AscendingStableBase,
public enum FileRollingConvention {
/// <summary>
/// (Default) New files will get an ascending rolling index, files get rolled after max 0-1-2-3-0-1-2-3.
/// </summary>
Ascending,
/// <summary>
/// New files will get an ascending rolling index, but the latest file is always the file without index. More performant alt for descending rolling. 0-1-2-3-1-2-3
/// </summary>
AscendingStableBase,
/// <summary>
/// Unix like descending logging, the base will always be stable and contain the latest logs, new files will be incremented and renamed so the highest number is always the oldest. 0-1-2-3
/// </summary>
Expand Down
98 changes: 45 additions & 53 deletions src/NReco.Logging.File/FileLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ public void Dispose() {
entryQueue.CompleteAdding();
try {
processQueueTask.Wait(1500); // the same as in ConsoleLogger
}
catch (TaskCanceledException) { }
catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException) { }
} catch (TaskCanceledException) {
} catch (AggregateException ex) when (ex.InnerExceptions.Count == 1 && ex.InnerExceptions[0] is TaskCanceledException) { }

loggers.Clear();
fWriter.Close();
Expand All @@ -124,8 +123,7 @@ internal void WriteEntry(string message) {
try {
entryQueue.Add(message);
return;
}
catch (InvalidOperationException) { }
} catch (InvalidOperationException) { }
}
// do nothing
}
Expand All @@ -135,8 +133,7 @@ private void ProcessQueue() {
try {
if (!writeMessageFailed)
fWriter.WriteMessage(message, entryQueue.Count == 0);
}
catch (Exception ex) {
} catch (Exception ex) {
// something goes wrong. App's code can handle it if 'HandleFileError' is provided
var stopLogging = true;
if (HandleFileError != null) {
Expand All @@ -149,8 +146,7 @@ private void ProcessQueue() {
fWriter.WriteMessage(message, entryQueue.Count == 0);
stopLogging = false;
}
}
catch {
} catch {
// exception is possible in HandleFileError or if proposed file name cannot be used
// let's ignore it in that case -> file logger will stop processing log messages
}
Expand Down Expand Up @@ -203,17 +199,14 @@ void DetermineLastFileLogName() {
.OrderByDescending(fInfo => fInfo.Name)
.OrderByDescending(fInfo => fInfo.LastWriteTime).First();
LogFileName = lastFileInfo.FullName;
}
else {
} else {
// no files yet, use default name
LogFileName = baseLogFileName;
}
}
else {
} else {
LogFileName = baseLogFileName;
}
}
else {
} else {
LogFileName = baseLogFileName;
}
}
Expand All @@ -227,8 +220,7 @@ void createLogFileStream(bool append) {
LogFileStream = new FileStream(LogFileName, FileMode.OpenOrCreate, FileAccess.Write);
if (append) {
LogFileStream.Seek(0, SeekOrigin.End);
}
else {
} else {
LogFileStream.SetLength(0); // clear the file
}
LogFileWriter = new StreamWriter(LogFileStream);
Expand All @@ -243,8 +235,7 @@ internal void UseNewLogFile(string newLogFileName) {
void OpenFile(bool append) {
try {
createLogFileStream(append);
}
catch (Exception ex) {
} catch (Exception ex) {
if (FileLogPrv.HandleFileError != null) {
var fileErr = new FileError(LogFileName, ex);
FileLogPrv.HandleFileError(fileErr);
Expand All @@ -267,45 +258,46 @@ string GetNextFileLogName() {
new System.IO.FileInfo(baseLogFileName).Length < FileLogPrv.FileSizeLimitBytes)
return baseLogFileName;

if (FileLogPrv.Options.RollingFilesConvention == FileLoggerOptions.FileRollingConvention.Ascending) {
//Unchanged default handling just optimized for performance and code reuse
int currentFileIndex = GetIndexFromFile(baseLogFileName, LogFileName);
var nextFileIndex = currentFileIndex + 1;
if (FileLogPrv.MaxRollingFiles > 0) {
nextFileIndex %= FileLogPrv.MaxRollingFiles;
}
return GetFileFromIndex(baseLogFileName, nextFileIndex);
}
else if (FileLogPrv.Options.RollingFilesConvention == FileLoggerOptions.FileRollingConvention.AscendingStableBase) {
//Move current base file to next rolling file number
RollingNumber++;
if (FileLogPrv.MaxRollingFiles > 0) {
RollingNumber %= FileLogPrv.MaxRollingFiles - 1;
}
var moveFile = GetFileFromIndex(baseLogFileName, RollingNumber + 1);
if (System.IO.File.Exists(moveFile)) {
System.IO.File.Delete(moveFile);
}
System.IO.File.Move(baseLogFileName, moveFile);
return baseLogFileName;
}
else if (FileLogPrv.Options.RollingFilesConvention == FileLoggerOptions.FileRollingConvention.Descending) {
//Move all existing files to index +1 except if they are > MaxRollingFiles
var logFiles = GetExistingLogFiles(baseLogFileName);
if (logFiles.Length > 0) {
foreach (var finfo in logFiles.OrderByDescending(fInfo => fInfo.Name)) {
var index = GetIndexFromFile(baseLogFileName, finfo.Name);
if (FileLogPrv.MaxRollingFiles > 0 && index >= FileLogPrv.MaxRollingFiles - 1) {
continue;
switch (FileLogPrv.Options.RollingFilesConvention) {
case FileLoggerOptions.FileRollingConvention.Ascending:
//Unchanged default handling just optimized for performance and code reuse
int currentFileIndex = GetIndexFromFile(baseLogFileName, LogFileName);
var nextFileIndex = currentFileIndex + 1;
if (FileLogPrv.MaxRollingFiles > 0) {
nextFileIndex %= FileLogPrv.MaxRollingFiles;
}
var moveFile = GetFileFromIndex(baseLogFileName, index + 1);
return GetFileFromIndex(baseLogFileName, nextFileIndex);
case FileLoggerOptions.FileRollingConvention.AscendingStableBase: {
//Move current base file to next rolling file number
RollingNumber++;
if (FileLogPrv.MaxRollingFiles > 0) {
RollingNumber %= FileLogPrv.MaxRollingFiles - 1;
}
var moveFile = GetFileFromIndex(baseLogFileName, RollingNumber + 1);
if (System.IO.File.Exists(moveFile)) {
System.IO.File.Delete(moveFile);
}
System.IO.File.Move(finfo.FullName, moveFile);
System.IO.File.Move(baseLogFileName, moveFile);
return baseLogFileName;
}
case FileLoggerOptions.FileRollingConvention.Descending: {
//Move all existing files to index +1 except if they are > MaxRollingFiles
var logFiles = GetExistingLogFiles(baseLogFileName);
if (logFiles.Length > 0) {
foreach (var finfo in logFiles.OrderByDescending(fInfo => fInfo.Name)) {
var index = GetIndexFromFile(baseLogFileName, finfo.Name);
if (FileLogPrv.MaxRollingFiles > 0 && index >= FileLogPrv.MaxRollingFiles - 1) {
continue;
}
var moveFile = GetFileFromIndex(baseLogFileName, index + 1);
if (System.IO.File.Exists(moveFile)) {
System.IO.File.Delete(moveFile);
}
System.IO.File.Move(finfo.FullName, moveFile);
}
}
return baseLogFileName;
}
}
return baseLogFileName;
}
throw new NotImplementedException("RollingFilesConvention");
}
Expand Down

0 comments on commit 4313f9c

Please sign in to comment.