Added log cleanup

This commit is contained in:
seerge
2023-03-14 14:21:20 +01:00
parent 89d7930323
commit a6cfc91de2
4 changed files with 30 additions and 19 deletions

View File

@@ -6,13 +6,12 @@ using System.Runtime.InteropServices;
using System.Text.Json;
public static class Logger
{
static string appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GHelper";
static string logFile = appPath + "\\log.txt";
public static void WriteLine(string logMessage)
{
Debug.WriteLine(logMessage);
var appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GHelper";
var logFile = appPath + "\\log.txt";
if (!Directory.Exists(appPath)) Directory.CreateDirectory(appPath);
using (StreamWriter w = File.AppendText(logFile))
@@ -20,6 +19,17 @@ public static class Logger
w.WriteLine($"{DateTime.Now}: {logMessage}");
}
var rand = new Random();
if (rand.Next(50) == 1) Cleanup();
}
public static void Cleanup()
{
var file = File.ReadAllLines(logFile);
int skip = Math.Max(0, file.Count() - 500);
File.WriteAllLines(logFile,file.Skip(skip).ToArray());
}
}