Adding new WinForms sample application (#452)

This commit is contained in:
Kevin B
2025-01-09 23:37:12 -08:00
committed by GitHub
parent 28c475284f
commit d462ed5479
8 changed files with 469 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
using System.Text;
using Microsoft.Extensions.Logging;
namespace CSharpWinForms;
public class LogUpdatedEventArgs : EventArgs
{
public string? Text { get; set; }
}
public class MemoryLogger : ILogger
{
public event EventHandler<LogUpdatedEventArgs>? LogUpdated;
private readonly StringBuilder _sb = new();
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> 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();
}
}
}