From 04b38454cc97357e8cb40efe47206261a9a41593 Mon Sep 17 00:00:00 2001 From: BWiegell Date: Tue, 29 Nov 2022 21:42:59 +0100 Subject: [PATCH] log pruning --- AstroWall/BusinessLayer/Logging.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/AstroWall/BusinessLayer/Logging.cs b/AstroWall/BusinessLayer/Logging.cs index ad64027..bc44da4 100644 --- a/AstroWall/BusinessLayer/Logging.cs +++ b/AstroWall/BusinessLayer/Logging.cs @@ -6,6 +6,9 @@ namespace AstroWall.BusinessLayer public class Logging { + // Formatting + private static string dateFormat = "dd/MM-yy--HH:mm:ss"; + //Singleton private static volatile Logging instance; private static object syncRoot = new object(); @@ -31,6 +34,7 @@ public static Logging Instance public Logging() { + pruneLogFile(); sw = File.AppendText(General.getLogPath()); } @@ -40,13 +44,20 @@ public static Action GetLogger(string caller, bool isError = false) { lock (syncRoot) { - string logStr = (isError ? "ERROR: " : "") + DateTime.Now.ToString("dd/MM-yy--HH:mm:ss") + ", " + caller + ": " + log; + string logStr = (isError ? "ERROR: " : "") + DateTime.Now.ToString(dateFormat) + ", " + caller + ": " + log; Logging.Instance.sw.WriteLine(logStr); Logging.Instance.sw.Flush(); Console.WriteLine(logStr); } }; } + + private void pruneLogFile() + { + string path = General.getLogPath(); + long size = (new FileInfo(path)).Length; + if (size > 5000000) File.Delete(path); + } } }