mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Added log cleanup
This commit is contained in:
18
Program.cs
18
Program.cs
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user