Undo Default constructor. Compiler cannot deal with it yet.

This commit is contained in:
ig 2023-08-16 15:38:46 +02:00
parent df9de4c9ed
commit 3d3a0375d2
1 changed files with 21 additions and 17 deletions

View File

@ -3,34 +3,38 @@ using Microsoft.Extensions.Logging;
namespace InnovEnergy.App.SaliMax; namespace InnovEnergy.App.SaliMax;
public class CustomLogger(String logFilePath, Int64 maxFileSizeBytes, Int32 maxLogFileCount) public class CustomLogger : ILogger
: ILogger
{ {
private Int64 _CurrentFileSizeBytes = File.Exists(logFilePath) private readonly String _LogFilePath;
? new FileInfo(logFilePath).Length private readonly Int64 _MaxFileSizeBytes;
: 0; private readonly Int32 _MaxLogFileCount;
private Int64 _CurrentFileSizeBytes;
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>(TState state) where TState : notnull => throw new NotImplementedException(); public IDisposable? BeginScope<TState>(TState state) where TState : notnull => throw new NotImplementedException();
public Boolean IsEnabled(LogLevel logLevel) => true; // Enable logging for all levels public Boolean IsEnabled(LogLevel logLevel) => true; // Enable logging for all levels
public void Log<TState>(LogLevel logLevel, public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception, String> formatter)
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception, String> formatter)
{ {
var logMessage = formatter(state, exception!); var logMessage = formatter(state, exception!);
// Check the file size and rotate the log file if necessary // Check the file size and rotate the log file if necessary
if (_CurrentFileSizeBytes + logMessage.Length >= maxFileSizeBytes) if (_CurrentFileSizeBytes + logMessage.Length >= _MaxFileSizeBytes)
{ {
RotateLogFile(); RotateLogFile();
_CurrentFileSizeBytes = 0; _CurrentFileSizeBytes = 0;
} }
// Write the log message to the file // Write the log message to the file
File.AppendAllText(logFilePath, logMessage + Environment.NewLine); File.AppendAllText(_LogFilePath, logMessage + Environment.NewLine);
_CurrentFileSizeBytes += logMessage.Length; _CurrentFileSizeBytes += logMessage.Length;
Console.WriteLine(logMessage); Console.WriteLine(logMessage);
@ -39,20 +43,20 @@ public class CustomLogger(String logFilePath, Int64 maxFileSizeBytes, Int32 maxL
private void RotateLogFile() private void RotateLogFile()
{ {
// Check the log file count and delete the oldest file if necessary // Check the log file count and delete the oldest file if necessary
var logFileDir = Path.GetDirectoryName(logFilePath)!; var logFileDir = Path.GetDirectoryName(_LogFilePath)!;
var logFileExt = Path.GetExtension(logFilePath); var logFileExt = Path.GetExtension(_LogFilePath);
var logFileBaseName = Path.GetFileNameWithoutExtension(logFilePath); var logFileBaseName = Path.GetFileNameWithoutExtension(_LogFilePath);
var logFiles = Directory var logFiles = Directory
.GetFiles(logFileDir, $"{logFileBaseName}_*{logFileExt}") .GetFiles(logFileDir, $"{logFileBaseName}_*{logFileExt}")
.OrderBy(file => file) .OrderBy(file => file)
.ToList(); .ToList();
if (logFiles.Count >= maxLogFileCount) if (logFiles.Count >= _MaxLogFileCount)
File.Delete(logFiles.First()); File.Delete(logFiles.First());
// Rename the current log file with a timestamp // Rename the current log file with a timestamp
var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{UnixTime.Now}{logFileExt}"); var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{UnixTime.Now}{logFileExt}");
File.Move(logFilePath, logFileBackupPath); File.Move(_LogFilePath, logFileBackupPath);
} }
} }