2023-06-13 11:01:48 +00:00
|
|
|
using InnovEnergy.Lib.Time.Unix;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.SaliMax;
|
|
|
|
|
2023-08-16 13:38:46 +00:00
|
|
|
public class CustomLogger : ILogger
|
2023-06-13 11:01:48 +00:00
|
|
|
{
|
2023-08-16 13:38:46 +00:00
|
|
|
private readonly String _LogFilePath;
|
|
|
|
private readonly Int64 _MaxFileSizeBytes;
|
|
|
|
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;
|
|
|
|
}
|
2023-06-13 11:01:48 +00:00
|
|
|
|
2023-08-16 13:11:37 +00:00
|
|
|
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => throw new NotImplementedException();
|
2023-06-13 11:01:48 +00:00
|
|
|
|
2023-08-16 13:11:37 +00:00
|
|
|
public Boolean IsEnabled(LogLevel logLevel) => true; // Enable logging for all levels
|
2023-06-13 11:01:48 +00:00
|
|
|
|
2023-08-16 13:38:46 +00:00
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception, String> formatter)
|
2023-06-13 11:01:48 +00:00
|
|
|
{
|
|
|
|
var logMessage = formatter(state, exception!);
|
|
|
|
|
|
|
|
// Check the file size and rotate the log file if necessary
|
2023-08-16 13:38:46 +00:00
|
|
|
if (_CurrentFileSizeBytes + logMessage.Length >= _MaxFileSizeBytes)
|
2023-06-13 11:01:48 +00:00
|
|
|
{
|
|
|
|
RotateLogFile();
|
2023-07-04 09:18:07 +00:00
|
|
|
_CurrentFileSizeBytes = 0;
|
2023-06-13 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the log message to the file
|
2023-08-16 13:38:46 +00:00
|
|
|
File.AppendAllText(_LogFilePath, logMessage + Environment.NewLine);
|
2023-07-04 09:18:07 +00:00
|
|
|
_CurrentFileSizeBytes += logMessage.Length;
|
2023-07-10 08:22:09 +00:00
|
|
|
|
2023-09-27 12:36:50 +00:00
|
|
|
//Console.WriteLine(logMessage);
|
2023-06-13 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void RotateLogFile()
|
|
|
|
{
|
|
|
|
// Check the log file count and delete the oldest file if necessary
|
2023-08-16 13:38:46 +00:00
|
|
|
var logFileDir = Path.GetDirectoryName(_LogFilePath)!;
|
|
|
|
var logFileExt = Path.GetExtension(_LogFilePath);
|
|
|
|
var logFileBaseName = Path.GetFileNameWithoutExtension(_LogFilePath);
|
2023-06-13 11:01:48 +00:00
|
|
|
|
2023-07-04 09:18:07 +00:00
|
|
|
var logFiles = Directory
|
|
|
|
.GetFiles(logFileDir, $"{logFileBaseName}_*{logFileExt}")
|
|
|
|
.OrderBy(file => file)
|
|
|
|
.ToList();
|
2023-06-13 11:01:48 +00:00
|
|
|
|
2023-08-16 13:38:46 +00:00
|
|
|
if (logFiles.Count >= _MaxLogFileCount)
|
2023-06-13 11:01:48 +00:00
|
|
|
File.Delete(logFiles.First());
|
|
|
|
|
|
|
|
// Rename the current log file with a timestamp
|
|
|
|
var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{UnixTime.Now}{logFileExt}");
|
2023-08-16 13:38:46 +00:00
|
|
|
File.Move(_LogFilePath, logFileBackupPath);
|
2023-06-13 11:01:48 +00:00
|
|
|
}
|
|
|
|
}
|