wabbajack/Wabbajack.App.Blazor/Components/VirtualLogger.razor
2022-01-28 12:58:28 +01:00

42 lines
1.2 KiB
Plaintext

@using NLog
@using NLog.Targets
@using Wabbajack.App.Blazor.Utility
@using System.Reactive.Linq
@namespace Wabbajack.App.Blazor.Components
@implements IDisposable
<div id="virtual-logger">
<Virtualize Items="_logs" Context="logItem" OverscanCount="5" ItemSize="24">
<span style="height: 24px">@logItem</span>
</Virtualize>
</div>
@code {
// TODO: [Low] More parameters to customise the logger. E.g. Reverse order.
// TODO: [High] Find a way to auto-scroll. (JS interop?)
private UiLoggerTarget? _loggerTarget;
private ICollection<string> _logs = new List<string>();
private bool _shouldRender = false;
protected override bool ShouldRender() => _shouldRender;
private IDisposable? _disposable;
protected override Task OnInitializedAsync()
{
_loggerTarget = LogManager.Configuration.FindTargetByName<UiLoggerTarget>("ui");
_disposable = _loggerTarget.Logs.Sample(TimeSpan.FromMilliseconds(250)).Subscribe(next =>
{
_logs.Add(next);
InvokeAsync(StateHasChanged);
});
_shouldRender = true;
return Task.CompletedTask;
}
public void Dispose() => _disposable?.Dispose();
}