Bring back C# sample logging!

This commit is contained in:
Caelan Sayler
2025-03-12 22:02:47 +00:00
committed by Caelan
parent 4f10c94565
commit a6337c5a22
7 changed files with 113 additions and 25 deletions

View File

@@ -0,0 +1,39 @@
using System;
using System.Text;
using Velopack.Logging;
namespace CSharpWpf;
public class LogUpdatedEventArgs : EventArgs
{
public string Text { get; set; }
}
public class MemoryLogger : IVelopackLogger
{
public event EventHandler<LogUpdatedEventArgs> LogUpdated;
private readonly StringBuilder _sb = new StringBuilder();
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public override string ToString()
{
lock (_sb) {
return _sb.ToString();
}
}
public void Log(VelopackLogLevel logLevel, string message, Exception exception)
{
lock (_sb) {
message = $"{logLevel}: {message}";
if (exception != null) message += "\n" + exception.ToString();
Console.WriteLine("log: " + message);
_sb.AppendLine(message);
LogUpdated?.Invoke(this, new LogUpdatedEventArgs { Text = _sb.ToString() });
}
}
}