Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Show correct log path in error dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Jun 8, 2022
1 parent 878fb99 commit 0aeb9fd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Squirrel.CommandLine/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using Squirrel.SimpleSplat;

namespace Squirrel.CommandLine
Expand All @@ -10,15 +11,18 @@ class ConsoleLogger : ILogger
private readonly object gate = new object();

private readonly string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
private readonly string localTemp = Path.GetTempPath();

public void Write(string message, LogLevel logLevel)
{
if (logLevel < Level) {
return;
}

if (SquirrelRuntimeInfo.IsWindows)
if (SquirrelRuntimeInfo.IsWindows) {
message = message.Replace(localTemp, "%temp%", StringComparison.InvariantCultureIgnoreCase);
message = message.Replace(localAppData, "%localappdata%", StringComparison.InvariantCultureIgnoreCase);
}

lock (gate) {
string lvl = logLevel.ToString().Substring(0, 4).ToUpper();
Expand Down
6 changes: 5 additions & 1 deletion src/Update.Windows/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class SetupLogLogger : ILogger
{
public LogLevel Level { get; set; } = LogLevel.Info;

public string LogFilePath { get; }

private readonly NLog.Logger _log;

public SetupLogLogger(UpdateAction action)
Expand All @@ -27,11 +29,13 @@ public SetupLogLogger(UpdateAction action)
: SquirrelRuntimeInfo.BaseDirectory;
var logName = logToTemp ? "Squirrel.log" : $"Squirrel-{action}.log";
var logArchiveName = logToTemp ? "Squirrel.{###}.log" : $"Squirrel-{action}.{{###}}.log";

LogFilePath = Path.Combine(logDirectory, logName);

// https://gist.github.com/chrisortman/1092889
SimpleConfigurator.ConfigureForTargetLogging(
new FileTarget() {
FileName = Path.Combine(logDirectory, logName),
FileName = LogFilePath,
Layout = new NLog.Layouts.SimpleLayout("${longdate} [${level:uppercase=true}] - ${message}"),
ArchiveFileName = Path.Combine(logDirectory, logArchiveName),
ArchiveAboveSize = 1_000_000,
Expand Down
10 changes: 6 additions & 4 deletions src/Update.Windows/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace Squirrel.Update
{
class Program : IEnableLogger
{
static StartupOption opt;
private static StartupOption opt;
private static SetupLogLogger _log;

static IFullLogger Log => SquirrelLocator.Current.GetService<ILogManager>().GetLogger(typeof(Program));

[STAThread]
Expand All @@ -42,8 +44,8 @@ static int main(string[] args)
try {
opt = new StartupOption(args);
} catch (Exception ex) {
var logp = new SetupLogLogger(UpdateAction.Unset);
logp.Write($"Failed to parse command line options. {ex.Message}", LogLevel.Error);
_log = new SetupLogLogger(UpdateAction.Unset);
_log.Write($"Failed to parse command line options. {ex.Message}", LogLevel.Error);
throw;
}

Expand Down Expand Up @@ -89,7 +91,7 @@ static int executeCommandLine(string[] args)
Windows.User32MessageBox.Show(
IntPtr.Zero,
"Setup encountered fatal error: " + ex.Message + Environment.NewLine
+ "There may be more detailed information in '%localappdata%\\SquirrelClowdTemp\\Squirrel.log'.",
+ $"There may be more detailed information in '{_log.LogFilePath}'.",
"Setup Error",
Windows.User32MessageBox.MessageBoxButtons.OK,
Windows.User32MessageBox.MessageBoxIcon.Error);
Expand Down

0 comments on commit 0aeb9fd

Please sign in to comment.