Update Logging Target

This commit is contained in:
erri120 2022-01-28 11:29:38 +01:00
parent a725688495
commit 2fab1b462f
No known key found for this signature in database
GPG Key ID: 7FA9556C936B847C
3 changed files with 37 additions and 10 deletions

View File

@ -40,12 +40,13 @@ public partial class App
Layout = "${processtime} [${level:uppercase=true}] (${logger}) ${message:withexception=true}",
Header = "############ Wabbajack log file - ${longdate} ############"
};
var consoleTarget = new ConsoleTarget("console");
var uiTarget = new MemoryTarget
var uiTarget = new UiLoggerTarget
{
Name = "ui",
Layout = "${message}",
};
var blackholeTarget = new NullTarget("blackhole");

View File

@ -1,23 +1,32 @@
@using NLog
@using NLog.Targets
@using Wabbajack.App.Blazor.Utility
@using System.Reactive.Linq
@namespace Wabbajack.App.Blazor.Components
<div id="virtual-logger">
<Virtualize Items="@Logs" Context="logItem" OverscanCount="3">
<span @key="logItem">@logItem</span>
</Virtualize>
@foreach (var logItem in _logs)
{
<span>@logItem</span>
}
</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 MemoryTarget? _memoryTarget;
private ICollection<string> Logs => _memoryTarget?.Logs ?? Array.Empty<string>();
private UiLoggerTarget? _loggerTarget;
private ICollection<string> _logs = Array.Empty<string>();
protected override void OnInitialized()
{
_memoryTarget = LogManager.Configuration.FindTargetByName<MemoryTarget>("ui");
{
_loggerTarget = LogManager.Configuration.FindTargetByName<UiLoggerTarget>("ui");
// TODO: can do different things here, buffering is just the simples operation
_loggerTarget.Logs.Buffer(TimeSpan.FromSeconds(1)).Subscribe(next =>
{
_logs = next;
InvokeAsync(StateHasChanged);
});
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Reactive.Subjects;
using NLog;
using NLog.Targets;
namespace Wabbajack.App.Blazor.Utility;
public class UiLoggerTarget : TargetWithLayout
{
private readonly Subject<string> _logs = new();
public IObservable<string> Logs => _logs;
protected override void Write(LogEventInfo logEvent)
{
_logs.OnNext(RenderLogEvent(Layout, logEvent));
}
}