From 908bc2a17c4215a39e2f80d4cc9a2670046960f7 Mon Sep 17 00:00:00 2001 From: Lorenz Cuno Klopfenstein Date: Fri, 31 Jan 2014 02:22:03 +0100 Subject: [PATCH] Improved logging with support for multiple instances (uses process ID). --- OnTopReplica/Log.cs | 12 ++++++++++-- OnTopReplica/Program.cs | 13 ++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/OnTopReplica/Log.cs b/OnTopReplica/Log.cs index 0dbeda1..104ed40 100644 --- a/OnTopReplica/Log.cs +++ b/OnTopReplica/Log.cs @@ -8,6 +8,7 @@ namespace OnTopReplica { static class Log { const string LogFileName = "lastrun.log.txt"; + const string ConflictLogFileName = "run-{0}.log.txt"; private readonly static StreamWriter Writer; @@ -18,8 +19,15 @@ namespace OnTopReplica { Writer.AutoFlush = true; } catch (Exception) { - //TODO: provide fallback logging mechanism? - Writer = null; + try { + 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; + } } } diff --git a/OnTopReplica/Program.cs b/OnTopReplica/Program.cs index 530d514..03fd60a 100644 --- a/OnTopReplica/Program.cs +++ b/OnTopReplica/Program.cs @@ -68,6 +68,8 @@ namespace OnTopReplica { using (_mainForm = new MainForm(options)) { Application.Idle += _handlerIdleUpdater; + Log.Write("Entering application loop"); + //Enter GUI loop Application.Run(_mainForm); @@ -80,7 +82,6 @@ namespace OnTopReplica { Log.Write("Last position before shutdown: {0}, size: {1}", _mainForm.Location, _mainForm.Size); Settings.Default.RestoreLastPosition = _mainForm.Location; Settings.Default.RestoreLastSize = _mainForm.ClientSize; - Settings.Default.Save(); //Store last thumbnail, if any if (_mainForm.ThumbnailPanel.IsShowingThumbnail && _mainForm.CurrentThumbnailWindowHandle != null) { @@ -93,7 +94,12 @@ namespace OnTopReplica { Settings.Default.RestoreLastWindowHwnd = 0; 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); @@ -136,14 +142,17 @@ namespace OnTopReplica { 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(); + sw.WriteLine("Last exception:"); sw.WriteLine(e.ExceptionObject.ToString()); sw.WriteLine(); + sw.WriteLine("Last log entries:"); foreach (var logEntry in Log.Queue) { sw.WriteLine(logEntry); } sw.WriteLine(); + sw.WriteLine("OnTopReplica v.{0}", Application.ProductVersion); sw.WriteLine("OS: {0}", Environment.OSVersion.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()); } } - - Log.Write("Crash dump written to {0}", path); } }