Improved logging with support for multiple instances (uses process ID).

This commit is contained in:
Lorenz Cuno Klopfenstein 2014-01-31 02:22:03 +01:00
parent 9d97da10c9
commit 908bc2a17c
2 changed files with 20 additions and 5 deletions

View file

@ -8,6 +8,7 @@ namespace OnTopReplica {
static class Log { static class Log {
const string LogFileName = "lastrun.log.txt"; const string LogFileName = "lastrun.log.txt";
const string ConflictLogFileName = "run-{0}.log.txt";
private readonly static StreamWriter Writer; private readonly static StreamWriter Writer;
@ -18,8 +19,15 @@ namespace OnTopReplica {
Writer.AutoFlush = true; Writer.AutoFlush = true;
} }
catch (Exception) { catch (Exception) {
//TODO: provide fallback logging mechanism? try {
Writer = null; var filepath = Path.Combine(AppPaths.PrivateRoamingFolderPath, string.Format(ConflictLogFileName, System.Diagnostics.Process.GetCurrentProcess().Id));
Writer = new StreamWriter(new FileStream(filepath, FileMode.Create));
Writer.AutoFlush = true;
}
catch (Exception) {
//No fallback logging possible
Writer = null;
}
} }
} }

View file

@ -68,6 +68,8 @@ namespace OnTopReplica {
using (_mainForm = new MainForm(options)) { using (_mainForm = new MainForm(options)) {
Application.Idle += _handlerIdleUpdater; Application.Idle += _handlerIdleUpdater;
Log.Write("Entering application loop");
//Enter GUI loop //Enter GUI loop
Application.Run(_mainForm); Application.Run(_mainForm);
@ -80,7 +82,6 @@ namespace OnTopReplica {
Log.Write("Last position before shutdown: {0}, size: {1}", _mainForm.Location, _mainForm.Size); Log.Write("Last position before shutdown: {0}, size: {1}", _mainForm.Location, _mainForm.Size);
Settings.Default.RestoreLastPosition = _mainForm.Location; Settings.Default.RestoreLastPosition = _mainForm.Location;
Settings.Default.RestoreLastSize = _mainForm.ClientSize; Settings.Default.RestoreLastSize = _mainForm.ClientSize;
Settings.Default.Save();
//Store last thumbnail, if any //Store last thumbnail, if any
if (_mainForm.ThumbnailPanel.IsShowingThumbnail && _mainForm.CurrentThumbnailWindowHandle != null) { if (_mainForm.ThumbnailPanel.IsShowingThumbnail && _mainForm.CurrentThumbnailWindowHandle != null) {
@ -93,7 +94,12 @@ namespace OnTopReplica {
Settings.Default.RestoreLastWindowHwnd = 0; Settings.Default.RestoreLastWindowHwnd = 0;
Settings.Default.RestoreLastWindowClass = string.Empty; Settings.Default.RestoreLastWindowClass = string.Empty;
} }
Log.Write("Persisting settings");
Settings.Default.Save();
} }
Log.Write("Shutting down OnTopReplica");
} }
private static EventHandler _handlerIdleUpdater = new EventHandler(Application_Idle); private static EventHandler _handlerIdleUpdater = new EventHandler(Application_Idle);
@ -136,14 +142,17 @@ namespace OnTopReplica {
sw.WriteLine("This file has been created because OnTopReplica crashed."); sw.WriteLine("This file has been created because OnTopReplica crashed.");
sw.WriteLine("Please send it to lck@klopfenstein.net to help fix the bug that caused the crash."); sw.WriteLine("Please send it to lck@klopfenstein.net to help fix the bug that caused the crash.");
sw.WriteLine(); sw.WriteLine();
sw.WriteLine("Last exception:"); sw.WriteLine("Last exception:");
sw.WriteLine(e.ExceptionObject.ToString()); sw.WriteLine(e.ExceptionObject.ToString());
sw.WriteLine(); sw.WriteLine();
sw.WriteLine("Last log entries:"); sw.WriteLine("Last log entries:");
foreach (var logEntry in Log.Queue) { foreach (var logEntry in Log.Queue) {
sw.WriteLine(logEntry); sw.WriteLine(logEntry);
} }
sw.WriteLine(); sw.WriteLine();
sw.WriteLine("OnTopReplica v.{0}", Application.ProductVersion); sw.WriteLine("OnTopReplica v.{0}", Application.ProductVersion);
sw.WriteLine("OS: {0}", Environment.OSVersion.ToString()); sw.WriteLine("OS: {0}", Environment.OSVersion.ToString());
sw.WriteLine(".NET: {0}", Environment.Version.ToString()); sw.WriteLine(".NET: {0}", Environment.Version.ToString());
@ -152,8 +161,6 @@ namespace OnTopReplica {
sw.WriteLine("UTC time: {0} {1}", DateTime.UtcNow.ToShortDateString(), DateTime.UtcNow.ToShortTimeString()); sw.WriteLine("UTC time: {0} {1}", DateTime.UtcNow.ToShortDateString(), DateTime.UtcNow.ToShortTimeString());
} }
} }
Log.Write("Crash dump written to {0}", path);
} }
} }