Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Commit

Permalink
Logger now uses one file per session rather than one giant dump
Browse files Browse the repository at this point in the history
- Old logs are deleted.
  • Loading branch information
Nadromar committed Mar 30, 2019
1 parent d8d2955 commit 2ca2224
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MultiTablePro/Data/License.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void ApiRequest(string path)
else if (apiResult.result.is_valid == 1)
{
// todo: Set all result properties to class properties
Key = apiResult.result.license_key.ToObject<string>());
Key = apiResult.result.license_key.ToObject<string>();
//...

// Set activelicense and run application
Expand Down
2 changes: 1 addition & 1 deletion MultiTablePro/HotKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void UnregisterHotKey(HotKey hotKey)
}
catch
{
Logger.Log("Attempting to unregister a hotke<y that was never registered.", Logger.Status.Error);
Logger.Log("Attempting to unregister a hotkey that was never registered.", Logger.Status.Error);
return;
}

Expand Down
41 changes: 39 additions & 2 deletions MultiTablePro/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -17,16 +18,27 @@ static Logger()
// Set log level
LogLevel = (Status)Properties.Settings.Default.LogLevel;

// Determine log file location
// Get working AppData Local directory
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MultiTablePro");
if (Debugger.IsAttached) // Seperate directory for debugger
path = Path.Combine(path, "Debug");

// Enter logs subdirectory & create if it doesn't exist.
path = Path.Combine(path, "Logs");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
LogFilePath = Path.Combine(path, "output.log");

// Create log filename with timestamp
LogFilePath = Path.Combine(path, string.Format("mtp-{0}.log", DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")));

// Start log writer
new Thread(() => StartLogWriter()).Start();

// Application exit event
Application.Current.Exit += Application_Exit;

// Clean log files older than 6 months
new Thread(() => CleanLogs()).Start();
}

public enum Status
Expand Down Expand Up @@ -85,5 +97,30 @@ private static void StartLogWriter()
}
}
}


// Removes log files older than 6 months
private static void CleanLogs()
{
string logsDir = Path.GetDirectoryName(LogFilePath);

// Ensure it's an MTP logfile, not a backup, not some important file that somehow ended up here
Regex rValidLogFile = new Regex(@"mtp-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.log");

// Find approperiate files to delete & delete them
Directory.EnumerateFiles(logsDir)
.Where(s => rValidLogFile.IsMatch(Path.GetFileName(s)))
.Where(s => File.GetCreationTime(s) < DateTime.Now.AddMonths(-6))
.ToList() // No foreach in IEnumerable, needs list
.ForEach(s => File.Delete(s));
}

private static void Application_Exit(object sender, ExitEventArgs e)
{
// Delete empty log files when application closes
// Also helps to have these empty logfiles as an indication of unclean shutdown
if (File.Exists(LogFilePath) && File.ReadAllText(LogFilePath) == "")
File.Delete(LogFilePath);
}
}
}

0 comments on commit 2ca2224

Please sign in to comment.