mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Fix: creating a locator should not affect static state
This commit is contained in:
@@ -57,6 +57,12 @@ namespace Velopack.Locators
|
||||
/// </summary>
|
||||
IProcessImpl Process { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Add a logger to the list of loggers. This will be used to log messages from Velopack.
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
void AddLogger(IVelopackLogger logger);
|
||||
|
||||
/// <summary>
|
||||
/// Finds .nupkg files in the PackagesDir and returns a list of ReleaseEntryName objects.
|
||||
/// </summary>
|
||||
|
||||
@@ -36,9 +36,6 @@ namespace Velopack.Locators
|
||||
/// <inheritdoc />
|
||||
public override string? Channel { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IVelopackLogger Log { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string? AppTempDir => CreateSubDirIfDoesNotExist(TempUtil.GetDefaultTempBaseDirectory(), AppId);
|
||||
|
||||
@@ -63,15 +60,13 @@ namespace Velopack.Locators
|
||||
if (!VelopackRuntimeInfo.IsLinux)
|
||||
throw new NotSupportedException($"Cannot instantiate {nameof(LinuxVelopackLocator)} on a non-linux system.");
|
||||
|
||||
var combinedLog = new CombinedVelopackLogger();
|
||||
combinedLog.Add(customLog);
|
||||
Log = combinedLog;
|
||||
CombinedLogger = new CombinedVelopackLogger(customLog);
|
||||
|
||||
Process = processImpl ??= new DefaultProcessImpl(combinedLog);
|
||||
Process = processImpl ??= new DefaultProcessImpl(CombinedLogger);
|
||||
var ourPath = processImpl.GetCurrentProcessPath();
|
||||
var currentProcessId = processImpl.GetCurrentProcessId();
|
||||
|
||||
using var initLog = new CachedVelopackLogger(combinedLog);
|
||||
using var initLog = new CachedVelopackLogger(CombinedLogger);
|
||||
initLog.Info($"Initializing {nameof(LinuxVelopackLocator)}");
|
||||
var logFilePath = Path.Combine(Path.GetTempPath(), DefaultLoggingFileName);
|
||||
|
||||
@@ -107,7 +102,7 @@ namespace Velopack.Locators
|
||||
|
||||
try {
|
||||
var fileLog = new FileVelopackLogger(logFilePath, currentProcessId);
|
||||
combinedLog.Add(fileLog);
|
||||
CombinedLogger.Add(fileLog);
|
||||
} catch (Exception ex) {
|
||||
initLog.Error("Unable to create file logger: " + ex);
|
||||
}
|
||||
|
||||
@@ -39,9 +39,6 @@ namespace Velopack.Locators
|
||||
/// <inheritdoc />
|
||||
public override string? PackagesDir => CreateSubDirIfDoesNotExist(CachesAppDir, "packages");
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IVelopackLogger Log { get; }
|
||||
|
||||
private string? CachesAppDir => CreateSubDirIfDoesNotExist(CachesVelopackDir, AppId);
|
||||
private string? CachesVelopackDir => CreateSubDirIfDoesNotExist(CachesDir, "velopack");
|
||||
private string? CachesDir => CreateSubDirIfDoesNotExist(LibraryDir, "Caches");
|
||||
@@ -60,15 +57,13 @@ namespace Velopack.Locators
|
||||
if (!VelopackRuntimeInfo.IsOSX)
|
||||
throw new NotSupportedException($"Cannot instantiate {nameof(OsxVelopackLocator)} on a non-osx system.");
|
||||
|
||||
var combinedLog = new CombinedVelopackLogger();
|
||||
combinedLog.Add(customLog);
|
||||
Log = combinedLog;
|
||||
CombinedLogger = new CombinedVelopackLogger(customLog);
|
||||
|
||||
Process = processImpl ??= new DefaultProcessImpl(combinedLog);
|
||||
Process = processImpl ??= new DefaultProcessImpl(CombinedLogger);
|
||||
var ourPath = processImpl.GetCurrentProcessPath();
|
||||
var currentProcessId = processImpl.GetCurrentProcessId();
|
||||
|
||||
using var initLog = new CachedVelopackLogger(combinedLog);
|
||||
using var initLog = new CachedVelopackLogger(CombinedLogger);
|
||||
initLog.Info($"Initializing {nameof(OsxVelopackLocator)}");
|
||||
|
||||
string logFolder = Path.GetTempPath();
|
||||
@@ -107,7 +102,7 @@ namespace Velopack.Locators
|
||||
try {
|
||||
var logFilePath = Path.Combine(logFolder, logFileName);
|
||||
var fileLog = new FileVelopackLogger(logFilePath, currentProcessId);
|
||||
combinedLog.Add(fileLog);
|
||||
CombinedLogger.Add(fileLog);
|
||||
} catch (Exception ex) {
|
||||
initLog.Error("Unable to create file logger: " + ex);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Velopack.Locators
|
||||
public abstract class VelopackLocator : IVelopackLocator
|
||||
{
|
||||
private static IVelopackLocator? _current;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The default log file name for Velopack.
|
||||
/// </summary>
|
||||
@@ -34,7 +34,8 @@ namespace Velopack.Locators
|
||||
get {
|
||||
if (_current == null)
|
||||
throw new InvalidOperationException(
|
||||
$"No VelopackLocator has been set. Either call {nameof(VelopackApp)}.{nameof(VelopackApp.Build)}() or provide {nameof(IVelopackLocator)} as a method parameter.");
|
||||
$"No VelopackLocator has been set. Either call {nameof(VelopackApp)}.{nameof(VelopackApp.Build)}().Run() " +
|
||||
$"or provide {nameof(IVelopackLocator)} as a method parameter.");
|
||||
return _current;
|
||||
}
|
||||
}
|
||||
@@ -43,13 +44,13 @@ namespace Velopack.Locators
|
||||
public static IVelopackLocator CreateDefaultForPlatform(IProcessImpl? processImpl = null, IVelopackLogger? logger = null)
|
||||
{
|
||||
if (VelopackRuntimeInfo.IsWindows)
|
||||
return _current = new WindowsVelopackLocator(processImpl, logger);
|
||||
return new WindowsVelopackLocator(processImpl, logger);
|
||||
|
||||
if (VelopackRuntimeInfo.IsOSX)
|
||||
return _current = new OsxVelopackLocator(processImpl, logger);
|
||||
return new OsxVelopackLocator(processImpl, logger);
|
||||
|
||||
if (VelopackRuntimeInfo.IsLinux)
|
||||
return _current = new LinuxVelopackLocator(processImpl, logger);
|
||||
return new LinuxVelopackLocator(processImpl, logger);
|
||||
|
||||
throw new PlatformNotSupportedException($"OS platform '{VelopackRuntimeInfo.SystemOs.GetOsLongName()}' is not supported.");
|
||||
}
|
||||
@@ -87,7 +88,7 @@ namespace Velopack.Locators
|
||||
public abstract string? Channel { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public abstract IVelopackLogger Log { get; }
|
||||
public virtual IVelopackLogger Log => ((IVelopackLogger?) CombinedLogger) ?? new NullVelopackLogger();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual bool IsPortable => false;
|
||||
@@ -111,6 +112,14 @@ namespace Velopack.Locators
|
||||
/// <inheritdoc/>
|
||||
public abstract SemanticVersion? CurrentlyInstalledVersion { get; }
|
||||
|
||||
internal CombinedVelopackLogger? CombinedLogger { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void AddLogger(IVelopackLogger logger)
|
||||
{
|
||||
CombinedLogger?.Add(logger);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual List<VelopackAsset> GetLocalPackages()
|
||||
{
|
||||
|
||||
@@ -37,9 +37,6 @@ namespace Velopack.Locators
|
||||
/// <inheritdoc />
|
||||
public override string? PackagesDir => _packagesDir.Value;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IVelopackLogger Log { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsPortable => RootAppDir != null && File.Exists(Path.Combine(RootAppDir, ".portable"));
|
||||
|
||||
@@ -56,16 +53,13 @@ namespace Velopack.Locators
|
||||
throw new NotSupportedException($"Cannot instantiate {nameof(WindowsVelopackLocator)} on a non-Windows system.");
|
||||
|
||||
_packagesDir = new(GetPackagesDir);
|
||||
CombinedLogger = new CombinedVelopackLogger(customLog);
|
||||
|
||||
var combinedLog = new CombinedVelopackLogger();
|
||||
combinedLog.Add(customLog);
|
||||
Log = combinedLog;
|
||||
|
||||
Process = processImpl ??= new DefaultProcessImpl(combinedLog);
|
||||
Process = processImpl ??= new DefaultProcessImpl(CombinedLogger);
|
||||
var ourPath = processImpl.GetCurrentProcessPath();
|
||||
var currentProcessId = processImpl.GetCurrentProcessId();
|
||||
|
||||
using var initLog = new CachedVelopackLogger(combinedLog);
|
||||
using var initLog = new CachedVelopackLogger(CombinedLogger);
|
||||
initLog.Info($"Initializing {nameof(WindowsVelopackLocator)}");
|
||||
|
||||
// We try various approaches here. Firstly, if Update.exe is in the parent directory,
|
||||
@@ -133,6 +127,7 @@ namespace Velopack.Locators
|
||||
Directory.CreateDirectory(TempAppRootDirectory);
|
||||
File.Copy(UpdateExePath, tempTargetUpdateExe);
|
||||
}
|
||||
|
||||
UpdateExePath = tempTargetUpdateExe;
|
||||
}
|
||||
|
||||
@@ -142,7 +137,7 @@ namespace Velopack.Locators
|
||||
try {
|
||||
var logFilePath = Path.Combine(RootAppDir, DefaultLoggingFileName);
|
||||
var fileLog = new FileVelopackLogger(logFilePath, currentProcessId);
|
||||
combinedLog.Add(fileLog);
|
||||
CombinedLogger.Add(fileLog);
|
||||
//fileLogCreated = true;
|
||||
} catch (Exception ex) {
|
||||
fileLogException = ex;
|
||||
@@ -156,7 +151,7 @@ namespace Velopack.Locators
|
||||
var logFileName = String.IsNullOrEmpty(AppId) ? DefaultLoggingFileName : $"velopack_{AppId}.log";
|
||||
var logFilePath = Path.Combine(Path.GetTempPath(), logFileName);
|
||||
var fileLog = new FileVelopackLogger(logFilePath, currentProcessId);
|
||||
combinedLog.Add(fileLog);
|
||||
CombinedLogger.Add(fileLog);
|
||||
} catch (Exception ex) {
|
||||
tempFileLogException = ex;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,15 @@ namespace Velopack.Logging
|
||||
{
|
||||
private readonly List<IVelopackLogger> _loggers = new();
|
||||
|
||||
public CombinedVelopackLogger(params IVelopackLogger?[] loggers)
|
||||
{
|
||||
foreach (var logger in loggers) {
|
||||
if (logger != null) {
|
||||
_loggers.Add(logger);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Log(VelopackLogLevel logLevel, string? message, Exception? exception)
|
||||
{
|
||||
foreach (var logger in _loggers) {
|
||||
|
||||
@@ -4,7 +4,7 @@ using Velopack;
|
||||
using Velopack.Locators;
|
||||
using Velopack.Logging;
|
||||
|
||||
var locator = VelopackLocator.CreateDefaultForPlatform(new ConsoleVelopackLogger());
|
||||
var locator = VelopackLocator.CreateDefaultForPlatform(logger: new ConsoleVelopackLogger());
|
||||
|
||||
try {
|
||||
bool shouldExit = false;
|
||||
|
||||
Reference in New Issue
Block a user