Skip to content

Commit

Permalink
Dump logs if asked to
Browse files Browse the repository at this point in the history
Log will be saved to the original app folder, unless we weren't able to get the DTO, in which case the log will be saved to the temp folder
  • Loading branch information
synhershko committed Jul 13, 2012
1 parent 0d984d7 commit 13ff650
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 54 deletions.
98 changes: 55 additions & 43 deletions src/NAppUpdate.Framework/Common/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace NAppUpdate.Framework.Common
{
public class Logger
{
public class Logger
{
[Serializable]
public enum SeverityLevel
{
Expand All @@ -13,30 +14,30 @@ public enum SeverityLevel
Error
}

[Serializable]
public class LogItem
{
public DateTime Timestamp { get; set; }
public string Message { get; set; }
public Exception Exception { get; set; }
public SeverityLevel Severity { get; set; }
[Serializable]
public class LogItem
{
public DateTime Timestamp { get; set; }
public string Message { get; set; }
public Exception Exception { get; set; }
public SeverityLevel Severity { get; set; }

public override string ToString()
{
if (Exception == null)
return string.Format("{0,-25}\t{1}\t{2}",
Timestamp.ToShortDateString() + " " + Timestamp.ToString("HH:mm:ss.fff"),
Severity,
Message);
public override string ToString()
{
if (Exception == null)
return string.Format("{0,-25}\t{1}\t{2}",
Timestamp.ToShortDateString() + " " + Timestamp.ToString("HH:mm:ss.fff"),
Severity,
Message);

return string.Format("{0,-25}\t{1}\t{2}{3}{4}",
Timestamp.ToShortDateString() + " " + Timestamp.ToString("HH:mm:ss.fff"),
Severity,
Message, Environment.NewLine, Exception);
}
}
return string.Format("{0,-25}\t{1}\t{2}{3}{4}",
Timestamp.ToShortDateString() + " " + Timestamp.ToString("HH:mm:ss.fff"),
Severity,
Message, Environment.NewLine, Exception);
}
}

public List<LogItem> LogItems { get; private set; }
public List<LogItem> LogItems { get; private set; }

public Logger()
{
Expand All @@ -48,30 +49,41 @@ public Logger(List<LogItem> logItems)
LogItems = logItems;
}

public void Log(SeverityLevel severity, string message, params object[] args)
{
LogItems.Add(new LogItem
{
Message = string.Format(message, args),
Severity = severity,
Timestamp = DateTime.Now,
});
}
public void Log(SeverityLevel severity, string message, params object[] args)
{
LogItems.Add(new LogItem
{
Message = string.Format(message, args),
Severity = severity,
Timestamp = DateTime.Now,
});
}

public void Log(Exception exception)
{
Log(exception, string.Empty);
}

public void Log(Exception exception, string message)
{
LogItems.Add(new LogItem
{
Message = message,
Severity = SeverityLevel.Error,
Timestamp = DateTime.Now,
Exception = exception,
});
}
}
public void Log(Exception exception, string message)
{
LogItems.Add(new LogItem
{
Message = message,
Severity = SeverityLevel.Error,
Timestamp = DateTime.Now,
Exception = exception,
});
}

public void Dump(string filePath)
{
foreach (var logItem in LogItems)
{
using (StreamWriter w = File.CreateText(filePath))
{
w.WriteLine(logItem.ToString());
}
}
}
}
}
23 changes: 12 additions & 11 deletions src/NAppUpdate.Updater/AppStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ private static void Main()

if (_args.Log)
{
// Setup a temporary location for the log file, until we can get the DTO
logFile = System.Reflection.Assembly.GetEntryAssembly().Location;
logFile = Path.Combine(Path.GetDirectoryName(logFile), @"logs\NauUpdate.log");

if (_args.ShowConsole)
{
_console.WriteLine("Logging to {0}", logFile);
_console.WriteLine();
}
logFile = Path.Combine(Path.GetDirectoryName(logFile), @"NauUpdate.log");
}

try
Expand Down Expand Up @@ -77,9 +72,7 @@ private static void Main()
bool updateSuccessful = true;

if (dto == null || dto.Configs == null)
{
throw new Exception("Invalid DTO received");
}

if (dto.LogItems != null) // shouldn't really happen
{
Expand All @@ -94,10 +87,11 @@ private static void Main()
string backupFolder = dto.Configs.BackupFolder;
bool relaunchApp = dto.RelaunchApplication;

if (!string.IsNullOrEmpty(dto.AppPath))
logFile = Path.Combine(dto.AppPath, @"NauUpdate.log"); // now we can log to a more accessible location

if (dto.Tasks == null || dto.Tasks.Count == 0)
{
throw new Exception("Could not find the updates list (or it was empty).");
}

Log("Got {0} task objects", dto.Tasks.Count);

Expand Down Expand Up @@ -175,8 +169,15 @@ private static void Main()
}
finally
{
if (_args.Log) _logger.Dump(logFile);
if (_args.ShowConsole)
{
if (_args.Log)
{
_console.WriteLine();
_console.WriteLine("Log file was saved to {0}", logFile);
_console.WriteLine();
}
_console.WriteLine();
_console.WriteLine("Press any key or close this window to exit.");
_console.ReadKey();
Expand Down

0 comments on commit 13ff650

Please sign in to comment.