Add log file limit to Logger.
This commit is contained in:
@@ -7,7 +7,12 @@ namespace DaggerFramework.Utils
|
|||||||
public class Logger
|
public class Logger
|
||||||
{
|
{
|
||||||
public static Action<string, LogLevel>? OnLog;
|
public static Action<string, LogLevel>? OnLog;
|
||||||
public static string LogPath = "Logs/";
|
public static string LogPath { get; set; } = "Logs/";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maximum amount of log files in a log folder. If it reaches the limit, all logs will be written to <c>dagger-latest.log</c> instead of creating a new one.
|
||||||
|
/// </summary>
|
||||||
|
public static int MaxLogFiles { get; set; } = 5;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies the logging level. In release builds, the log level is <c>Error</c>. In debug, the log level is <c>Echo</c>.
|
/// Specifies the logging level. In release builds, the log level is <c>Error</c>. In debug, the log level is <c>Echo</c>.
|
||||||
@@ -24,9 +29,24 @@ namespace DaggerFramework.Utils
|
|||||||
|
|
||||||
if (WriteToFile && !_logCreated)
|
if (WriteToFile && !_logCreated)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(LogPath);
|
var dirInfo = Directory.CreateDirectory(LogPath);
|
||||||
|
var files = dirInfo.GetFiles();
|
||||||
|
|
||||||
string logName = $"dagger-{DateFormat}-{TimeFormat}.log".Replace(':', '.');
|
string logName = $"dagger-{DateFormat}-{TimeFormat}.log".Replace(':', '.');
|
||||||
_fileStream = File.Create(Path.Combine(LogPath, logName));
|
|
||||||
|
if (files.Length >= MaxLogFiles)
|
||||||
|
{
|
||||||
|
logName = "dagger-latest.log";
|
||||||
|
}
|
||||||
|
|
||||||
|
var path = Path.Combine(LogPath, logName);
|
||||||
|
|
||||||
|
if (File.Exists(path))
|
||||||
|
{
|
||||||
|
File.Delete(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
_fileStream = File.Create(path);
|
||||||
_fileWriter = new StreamWriter(_fileStream);
|
_fileWriter = new StreamWriter(_fileStream);
|
||||||
_logCreated = true;
|
_logCreated = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user