mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
40 lines
959 B
C#
40 lines
959 B
C#
using System.Diagnostics;
|
|
|
|
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);
|
|
if (!Directory.Exists(appPath)) Directory.CreateDirectory(appPath);
|
|
|
|
try
|
|
{
|
|
using (StreamWriter w = File.AppendText(logFile))
|
|
{
|
|
w.WriteLine($"{DateTime.Now}: {logMessage}");
|
|
w.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
if (new Random().Next(100) == 1) Cleanup();
|
|
|
|
|
|
}
|
|
|
|
public static void Cleanup()
|
|
{
|
|
try
|
|
{
|
|
var file = File.ReadAllLines(logFile);
|
|
int skip = Math.Max(0, file.Count() - 500);
|
|
File.WriteAllLines(logFile, file.Skip(skip).ToArray());
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
}
|