0

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.

2
  • Are you saying that _allLogs.Add is not called? When? Are you sure you have not disposed logDisposable? Commented Apr 24, 2024 at 13:38
  • I have done more tests and changed the question a bit. There does no dispose happen, the actual problem seems to be that the LogLevel filter is only applied to any new items added. So If I change the setting to lets say warning, there are still debug log entries in the filtered list, but no new ones get added. Commented Apr 24, 2024 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.