I have a logger with a OnLog event and want to display these log entries in a Panel. The panel has some RadioButtons to select the minimum diplayed log level.
private readonly ReadOnlyObservableCollection<ILogEventArgs> _filteredLogs;
private readonly SourceList<ILogEventArgs> _allLogs = new();
public LogPanelViewModel(ILogger logger, IConfigurationService<ILogPanelConfiguration> configurationService)
{
Logger = logger;
LogLevel = configurationService.Configuration.LogLevel ?? LogLevel.Info;
var logDisposable = Observable.FromEventPattern<ILogEventArgs>(
handler => logger.OnLog += handler,
handler => logger.OnLog -= handler)
.Select(eventPattern => eventPattern.EventArgs)
.Do(_allLogs.Add)
.Subscribe();
Cleanup.Add(logDisposable);
var filteredDisposable = _allLogs.Connect()
.Filter(x => x.LogLevel >= LogLevel)
.Bind(out _filteredLogs)
.Subscribe();
Cleanup.Add(filteredDisposable);
}
public LogLevel LogLevel
{
get => _logLevel;
set => this.RaiseAndSetIfChanged(ref _logLevel, value);
}
public ReadOnlyObservableCollection<ILogEventArgs> FilteredLogs => _filteredLogs;
Edit:
If I change the LogLevel the list with log messages does not get updated. It affects only if the new items are added. For the whole list it should be reevaluated if the entries are added to FilteredList.
Cleanup is a CompositeDisposable in my viewmodel to collect the stuff and dispose it if I navigate.
I tried to integrate WhenAnyChanged into this fluent chain but the property is not found there.
I know how I could make this run without ReactiveUI, but I would like to use it.
I use AvaloniaUI, but for the ViewModel it makes no difference and would be the same with WPF.
_allLogs.Addis not called? When? Are you sure you have not disposedlogDisposable?