diff --git a/samples/CSharpAvalonia/CSharpAvalonia.csproj b/samples/CSharpAvalonia/CSharpAvalonia.csproj index 5bdd1968..4ce47ef3 100644 --- a/samples/CSharpAvalonia/CSharpAvalonia.csproj +++ b/samples/CSharpAvalonia/CSharpAvalonia.csproj @@ -19,8 +19,7 @@ - - + diff --git a/samples/CSharpAvalonia/MainWindow.axaml.cs b/samples/CSharpAvalonia/MainWindow.axaml.cs index d85b512c..2fb336cb 100644 --- a/samples/CSharpAvalonia/MainWindow.axaml.cs +++ b/samples/CSharpAvalonia/MainWindow.axaml.cs @@ -3,7 +3,6 @@ using System.Text; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Threading; -using Microsoft.Extensions.Logging; using Velopack; namespace CSharpAvalonia; @@ -18,10 +17,8 @@ public partial class MainWindow : Window InitializeComponent(); var updateUrl = SampleHelper.GetReleasesDir(); // replace with your update path/url - _um = new UpdateManager(updateUrl, logger: Program.Log); + _um = new UpdateManager(updateUrl); - TextLog.Text = Program.Log.ToString(); - Program.Log.LogUpdated += LogUpdated; UpdateStatus(); } @@ -32,8 +29,9 @@ public partial class MainWindow : Window // ConfigureAwait(true) so that UpdateStatus() is called on the UI thread _update = await _um.CheckForUpdatesAsync().ConfigureAwait(true); } catch (Exception ex) { - Program.Log.LogError(ex, "Error checking for updates"); + LogMessage("Error checking for updates", ex); } + UpdateStatus(); } @@ -44,8 +42,9 @@ public partial class MainWindow : Window // ConfigureAwait(true) so that UpdateStatus() is called on the UI thread await _um.DownloadUpdatesAsync(_update, Progress).ConfigureAwait(true); } catch (Exception ex) { - Program.Log.LogError(ex, "Error downloading updates"); + LogMessage("Error downloading updates", ex); } + UpdateStatus(); } @@ -54,26 +53,32 @@ public partial class MainWindow : Window _um.ApplyUpdatesAndRestart(_update); } - private void LogUpdated(object sender, LogUpdatedEventArgs e) + private void LogMessage(string text, Exception e = null) { // logs can be sent from other threads - Dispatcher.UIThread.InvokeAsync(() => { - TextLog.Text = e.Text; - ScrollLog.ScrollToEnd(); - }); + Dispatcher.UIThread.InvokeAsync( + () => { + TextLog.Text += text + Environment.NewLine; + if (e != null) { + TextLog.Text += e.ToString() + Environment.NewLine; + } + + ScrollLog.ScrollToEnd(); + }); } private void Progress(int percent) { // progress can be sent from other threads - Dispatcher.UIThread.InvokeAsync(() => { - TextStatus.Text = $"Downloading ({percent}%)..."; - }); + Dispatcher.UIThread.InvokeAsync( + () => { + TextStatus.Text = $"Downloading ({percent}%)..."; + }); } private void Working() { - Program.Log.LogInformation(""); + LogMessage(""); BtnCheckUpdate.IsEnabled = false; BtnDownloadUpdate.IsEnabled = false; BtnRestartApply.IsEnabled = false; diff --git a/samples/CSharpAvalonia/MemoryLogger.cs b/samples/CSharpAvalonia/MemoryLogger.cs deleted file mode 100644 index 8786f511..00000000 --- a/samples/CSharpAvalonia/MemoryLogger.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Text; -using Microsoft.Extensions.Logging; - -namespace CSharpAvalonia; - -public class LogUpdatedEventArgs : EventArgs -{ - public string Text { get; set; } -} - -public class MemoryLogger : ILogger -{ - public event EventHandler LogUpdated; - private readonly StringBuilder _sb = new StringBuilder(); - - public IDisposable BeginScope(TState state) - { - return null; - } - - public bool IsEnabled(LogLevel logLevel) - { - return true; - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) - { - lock (_sb) { - var message = formatter(state, exception); - if (exception != null) message += "\n" + exception.ToString(); - Console.WriteLine("log: " + message); - _sb.AppendLine(message); - LogUpdated?.Invoke(this, new LogUpdatedEventArgs { Text = _sb.ToString() }); - } - } - - public override string ToString() - { - lock (_sb) { - return _sb.ToString(); - } - } -} diff --git a/samples/CSharpAvalonia/Program.cs b/samples/CSharpAvalonia/Program.cs index 2d643306..bfd77e02 100644 --- a/samples/CSharpAvalonia/Program.cs +++ b/samples/CSharpAvalonia/Program.cs @@ -6,8 +6,6 @@ namespace CSharpAvalonia; class Program { - public static MemoryLogger Log { get; private set; } - // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. @@ -15,13 +13,10 @@ class Program public static void Main(string[] args) { try { - // Logging is essential for debugging! Ideally you should write it to a file. - Log = new MemoryLogger(); - // It's important to Run() the VelopackApp as early as possible in app startup. VelopackApp.Build() - .WithFirstRun((v) => { /* Your first run code here */ }) - .Run(Log); + .OnFirstRun((v) => { /* Your first run code here */ }) + .Run(); // Now it's time to run Avalonia BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); diff --git a/samples/CSharpWpf/App.xaml.cs b/samples/CSharpWpf/App.xaml.cs index e1cc3bbb..1a9f4e83 100644 --- a/samples/CSharpWpf/App.xaml.cs +++ b/samples/CSharpWpf/App.xaml.cs @@ -5,8 +5,6 @@ namespace CSharpWpf { public partial class App : Application { - public static MemoryLogger Log { get; private set; } - // Since WPF has an "automatic" Program.Main, we need to create our own. // In order for this to work, you must also add the following to your .csproj: // CSharpWpf.App @@ -14,13 +12,10 @@ namespace CSharpWpf private static void Main(string[] args) { try { - // Logging is essential for debugging! Ideally you should write it to a file. - Log = new MemoryLogger(); - // It's important to Run() the VelopackApp as early as possible in app startup. VelopackApp.Build() - .WithFirstRun((v) => { /* Your first run code here */ }) - .Run(Log); + .OnFirstRun((v) => { /* Your first run code here */ }) + .Run(); // We can now launch the WPF application as normal. var app = new App(); diff --git a/samples/CSharpWpf/CSharpWpf.csproj b/samples/CSharpWpf/CSharpWpf.csproj index f0634209..5d21eb4e 100644 --- a/samples/CSharpWpf/CSharpWpf.csproj +++ b/samples/CSharpWpf/CSharpWpf.csproj @@ -19,8 +19,7 @@ - - + diff --git a/samples/CSharpWpf/MainWindow.xaml.cs b/samples/CSharpWpf/MainWindow.xaml.cs index 1ad9f2f2..cb0fe328 100644 --- a/samples/CSharpWpf/MainWindow.xaml.cs +++ b/samples/CSharpWpf/MainWindow.xaml.cs @@ -1,6 +1,5 @@ using System.Text; using System.Windows; -using Microsoft.Extensions.Logging; using Velopack; namespace CSharpWpf @@ -15,10 +14,8 @@ namespace CSharpWpf InitializeComponent(); string updateUrl = SampleHelper.GetReleasesDir(); // replace with your update url - _um = new UpdateManager(updateUrl, logger: App.Log); + _um = new UpdateManager(updateUrl); - TextLog.Text = App.Log.ToString(); - App.Log.LogUpdated += LogUpdated; UpdateStatus(); } @@ -29,7 +26,7 @@ namespace CSharpWpf // ConfigureAwait(true) so that UpdateStatus() is called on the UI thread _update = await _um.CheckForUpdatesAsync().ConfigureAwait(true); } catch (Exception ex) { - App.Log.LogError(ex, "Error checking for updates"); + LogMessage("Error checking for updates", ex); } UpdateStatus(); } @@ -41,7 +38,7 @@ namespace CSharpWpf // ConfigureAwait(true) so that UpdateStatus() is called on the UI thread await _um.DownloadUpdatesAsync(_update, Progress).ConfigureAwait(true); } catch (Exception ex) { - App.Log.LogError(ex, "Error downloading updates"); + LogMessage("Error downloading updates", ex); } UpdateStatus(); } @@ -51,11 +48,14 @@ namespace CSharpWpf _um.ApplyUpdatesAndRestart(_update); } - private void LogUpdated(object sender, LogUpdatedEventArgs e) + private void LogMessage(string text, Exception e = null) { // logs can be sent from other threads this.Dispatcher.InvokeAsync(() => { - TextLog.Text = e.Text; + TextLog.Text += text + Environment.NewLine; + if (e != null) { + TextLog.Text += e.ToString() + Environment.NewLine; + } ScrollLog.ScrollToEnd(); }); } @@ -70,7 +70,7 @@ namespace CSharpWpf private void Working() { - App.Log.LogInformation(""); + LogMessage(""); BtnCheckUpdate.IsEnabled = false; BtnDownloadUpdate.IsEnabled = false; BtnRestartApply.IsEnabled = false; diff --git a/samples/CSharpWpf/MemoryLogger.cs b/samples/CSharpWpf/MemoryLogger.cs deleted file mode 100644 index 7852bee1..00000000 --- a/samples/CSharpWpf/MemoryLogger.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Text; -using Microsoft.Extensions.Logging; - -namespace CSharpWpf -{ - public class LogUpdatedEventArgs : EventArgs - { - public string Text { get; set; } - } - - public class MemoryLogger : ILogger - { - public event EventHandler LogUpdated; - private readonly StringBuilder _sb = new StringBuilder(); - - public IDisposable BeginScope(TState state) - { - return null; - } - - public bool IsEnabled(LogLevel logLevel) - { - return true; - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) - { - lock (_sb) { - var message = formatter(state, exception); - if (exception != null) message += "\n" + exception.ToString(); - Console.WriteLine("log: " + message); - _sb.AppendLine(message); - LogUpdated?.Invoke(this, new LogUpdatedEventArgs { Text = _sb.ToString() }); - } - } - - public override string ToString() - { - lock (_sb) { - return _sb.ToString(); - } - } - } -} diff --git a/samples/Directory.Build.targets b/samples/Directory.Build.targets index 973ce80d..3943538e 100644 --- a/samples/Directory.Build.targets +++ b/samples/Directory.Build.targets @@ -17,16 +17,5 @@ - - - https://api.nuget.org/v3/index.json;$(RestoreSources) - - $([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\build\$(Configuration)'));$(RestoreSources) - - - - - -