2022-05-22 04:27:02 +00:00
|
|
|
using System;
|
2022-05-23 13:07:05 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Reactive.Disposables;
|
2022-10-04 04:43:21 +00:00
|
|
|
using System.Reactive.Linq;
|
2022-05-22 04:27:02 +00:00
|
|
|
using System.Reactive.Subjects;
|
|
|
|
using System.Text;
|
2022-05-23 13:07:05 +00:00
|
|
|
using System.Windows.Data;
|
2022-05-22 04:27:02 +00:00
|
|
|
using DynamicData;
|
2022-05-23 13:07:05 +00:00
|
|
|
using DynamicData.Binding;
|
2022-05-22 04:27:02 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NLog;
|
|
|
|
using NLog.Targets;
|
2022-08-20 14:26:54 +00:00
|
|
|
using ReactiveUI;
|
|
|
|
using Wabbajack.Extensions;
|
2022-05-22 04:27:02 +00:00
|
|
|
using LogLevel = NLog.LogLevel;
|
|
|
|
|
|
|
|
namespace Wabbajack.Models;
|
|
|
|
|
|
|
|
public class LogStream : TargetWithLayout
|
|
|
|
{
|
|
|
|
|
2022-05-23 13:07:05 +00:00
|
|
|
private readonly SourceCache<ILogMessage, long> _messageLog = new(x => x.MessageId);
|
|
|
|
private readonly Subject<ILogMessage> _messages = new();
|
|
|
|
|
|
|
|
public readonly ReadOnlyObservableCollection<ILogMessage> _messagesFiltered;
|
|
|
|
private readonly CompositeDisposable _disposables;
|
|
|
|
public ReadOnlyObservableCollection<ILogMessage> MessageLog => _messagesFiltered;
|
|
|
|
public IObservable<ILogMessage> Messages => _messages;
|
|
|
|
|
|
|
|
|
|
|
|
public LogStream()
|
|
|
|
{
|
|
|
|
_disposables = new CompositeDisposable();
|
|
|
|
_messageLog.Connect()
|
2022-10-04 04:43:21 +00:00
|
|
|
.LimitSizeTo(200)
|
2022-05-23 13:07:05 +00:00
|
|
|
.Bind(out _messagesFiltered)
|
|
|
|
.Subscribe()
|
|
|
|
.DisposeWith(_disposables);
|
2022-10-04 04:43:21 +00:00
|
|
|
|
2022-05-23 13:07:05 +00:00
|
|
|
Messages
|
2022-08-20 14:26:54 +00:00
|
|
|
.Subscribe(m =>
|
|
|
|
{
|
|
|
|
RxApp.MainThreadScheduler.Schedule(m, (_, message) =>
|
|
|
|
{
|
|
|
|
_messageLog.AddOrUpdate(message);
|
|
|
|
return Disposable.Empty;
|
|
|
|
});
|
|
|
|
})
|
2022-05-23 13:07:05 +00:00
|
|
|
.DisposeWith(_disposables);
|
|
|
|
|
|
|
|
_messages.DisposeWith(_disposables);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
_disposables.Dispose();
|
|
|
|
}
|
2022-05-22 04:27:02 +00:00
|
|
|
|
|
|
|
protected override void Write(LogEventInfo logEvent)
|
|
|
|
{
|
2022-05-23 13:07:05 +00:00
|
|
|
_messages.OnNext(new LogMessage(logEvent));
|
2022-05-22 04:27:02 +00:00
|
|
|
}
|
|
|
|
public interface ILogMessage
|
|
|
|
{
|
|
|
|
long MessageId { get; }
|
|
|
|
|
|
|
|
string ShortMessage { get; }
|
|
|
|
DateTime TimeStamp { get; }
|
|
|
|
string LongMessage { get; }
|
|
|
|
}
|
|
|
|
|
|
|
|
private record LogMessage(LogEventInfo info) : ILogMessage
|
|
|
|
{
|
|
|
|
public long MessageId => info.SequenceID;
|
2022-05-23 13:07:05 +00:00
|
|
|
public string ShortMessage => info.FormattedMessage;
|
2022-05-22 04:27:02 +00:00
|
|
|
public DateTime TimeStamp => info.TimeStamp;
|
|
|
|
public string LongMessage => info.FormattedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|