private void LogFromUIThread(string message, LogViewLoggingLevel level) { string logEntry = $"{DateTime.Now:HH:mm:ss.fff} - {message}"; if (LoggingLevel <= level) { SolidColorBrush foregroundBrush = LoggingBrushes[(int)level]; if (IsLogViewFull) { // Once the view is full, messages go only to the debug console. System.Diagnostics.Debug.WriteLine(logEntry); return; } if (LoggingListBox.Items.Count >= MaxItemsInLogView) { IsLogViewFull = true; System.Diagnostics.Debug.WriteLine(logEntry); logEntry = "Log window is full"; foregroundBrush = new SolidColorBrush(Colors.Red); } LoggingListBox.Items.Insert(0, new TextBlock() { Text = logEntry, Foreground = foregroundBrush }); } else if (level != LogViewLoggingLevel.Verbose) { // Non-Verbose messages blocked from the LogView are shown on the debug console. System.Diagnostics.Debug.WriteLine($"{level}: {logEntry}"); } }
/// <summary> /// Provides logging with filtering based on UI settings. /// Logging functions tangential to main scenarios should specify a log view level less than Always. /// </summary> /// <param name="message">message to be logged</param> /// <param name="level">message category</param> public async void Log(string message, LogViewLoggingLevel level = LogViewLoggingLevel.Always) { if (Dispatcher.HasThreadAccess) { LogFromUIThread(message, level); } else { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { LogFromUIThread(message, level); }); } }
/// <summary> /// Changes the logging level at which messages are filtered. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LogLevel_Changed(object sender, SelectionChangedEventArgs e) { LoggingLevel = (LogViewLoggingLevel)LogLevelComboBox.SelectedIndex; }