From 9b4b947569a5caa408d5ba4d331f2b3d22f92d29 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 16 Aug 2023 15:11:37 +0200 Subject: [PATCH] use primary constructor --- csharp/App/SaliMax/src/Logfile.cs | 50 +++++++++++++------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/csharp/App/SaliMax/src/Logfile.cs b/csharp/App/SaliMax/src/Logfile.cs index 6465109df..fb7c18f65 100644 --- a/csharp/App/SaliMax/src/Logfile.cs +++ b/csharp/App/SaliMax/src/Logfile.cs @@ -3,66 +3,56 @@ using Microsoft.Extensions.Logging; namespace InnovEnergy.App.SaliMax; -public class CustomLogger : ILogger +public class CustomLogger(String logFilePath, Int64 maxFileSizeBytes, Int32 maxLogFileCount) + : ILogger { - private readonly String _LogFilePath; - private readonly Int64 _MaxFileSizeBytes; - private readonly Int32 _MaxLogFileCount; - private Int64 _CurrentFileSizeBytes; + private Int64 _CurrentFileSizeBytes = File.Exists(logFilePath) + ? new FileInfo(logFilePath).Length + : 0; - public CustomLogger(String logFilePath, Int64 maxFileSizeBytes, Int32 maxLogFileCount) - { - _LogFilePath = logFilePath; - _MaxFileSizeBytes = maxFileSizeBytes; - _MaxLogFileCount = maxLogFileCount; - _CurrentFileSizeBytes = File.Exists(logFilePath) ? new FileInfo(logFilePath).Length : 0; - } + public IDisposable? BeginScope(TState state) where TState : notnull => throw new NotImplementedException(); - public IDisposable? BeginScope(TState state) where TState : notnull - { - throw new NotImplementedException(); - } + public Boolean IsEnabled(LogLevel logLevel) => true; // Enable logging for all levels - public Boolean IsEnabled(LogLevel logLevel) - { - return true; // Enable logging for all levels - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + public void Log(LogLevel logLevel, + EventId eventId, + TState state, + Exception? exception, + Func formatter) { var logMessage = formatter(state, exception!); // Check the file size and rotate the log file if necessary - if (_CurrentFileSizeBytes + logMessage.Length >= _MaxFileSizeBytes) + if (_CurrentFileSizeBytes + logMessage.Length >= maxFileSizeBytes) { RotateLogFile(); _CurrentFileSizeBytes = 0; } // Write the log message to the file - File.AppendAllText(_LogFilePath, logMessage + Environment.NewLine); + File.AppendAllText(logFilePath, logMessage + Environment.NewLine); _CurrentFileSizeBytes += logMessage.Length; - Console.WriteLine( logMessage + Environment.NewLine); + Console.WriteLine(logMessage); } private void RotateLogFile() { // Check the log file count and delete the oldest file if necessary - var logFileDir = Path.GetDirectoryName(_LogFilePath)!; - var logFileExt = Path.GetExtension(_LogFilePath); - var logFileBaseName = Path.GetFileNameWithoutExtension(_LogFilePath); + var logFileDir = Path.GetDirectoryName(logFilePath)!; + var logFileExt = Path.GetExtension(logFilePath); + var logFileBaseName = Path.GetFileNameWithoutExtension(logFilePath); var logFiles = Directory .GetFiles(logFileDir, $"{logFileBaseName}_*{logFileExt}") .OrderBy(file => file) .ToList(); - if (logFiles.Count >= _MaxLogFileCount) + if (logFiles.Count >= maxLogFileCount) File.Delete(logFiles.First()); // Rename the current log file with a timestamp var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{UnixTime.Now}{logFileExt}"); - File.Move(_LogFilePath, logFileBackupPath); + File.Move(logFilePath, logFileBackupPath); } }