123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using H.Pipes;
- using InABox.Logging;
- using InABox.Wpf;
- namespace PRSDesktop
- {
- public class PropertyChangedBase : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- Application.Current.Dispatcher.BeginInvoke((Action)(() =>
- {
- var handler = PropertyChanged;
- if (handler != null)
- handler(this, new PropertyChangedEventArgs(propertyName));
- }));
- }
- }
- public class LogEntry : PropertyChangedBase
- {
- public string DateTime { get; set; }
- public string Type { get; set; }
- public string User { get; set; }
- public string Message { get; set; }
- }
- public class CollapsibleLogEntry : LogEntry
- {
- public List<LogEntry> Contents { get; set; }
- }
- public static class ItemsControlExtensions
- {
- public static void ScrollIntoView(
- this ItemsControl control,
- object item)
- {
- var framework =
- control.ItemContainerGenerator.ContainerFromItem(item)
- as FrameworkElement;
- if (framework == null) return;
- framework.BringIntoView();
- }
- public static void ScrollIntoView(this ItemsControl control)
- {
- var count = control.Items.Count;
- if (count == 0) return;
- var item = control.Items[count - 1];
- control.ScrollIntoView(item);
- }
- }
- /// <summary>
- /// Interaction logic for Console.xaml
- /// </summary>
- public partial class Console : ThemableWindow
- {
- //private PipeClient<string> _client;
- public CollectionViewSource _filtered;
- private readonly ObservableCollection<LogEntry> _logentries;
- private bool _monitoronly = true;
- private EventLogger logger;
- private int _msgindex = 1;
- public Console()
- {
- InitializeComponent();
- _logentries = new ObservableCollection<LogEntry>();
- _filtered = new CollectionViewSource();
- _filtered.Source = _logentries;
- _filtered.Filter += (sender, args) =>
- {
- args.Accepted = string.IsNullOrWhiteSpace(Search.Text)
- || ((LogEntry)args.Item).DateTime.Contains(Search.Text)
- || ((LogEntry)args.Item).Type.Contains(Search.Text)
- || ((LogEntry)args.Item).User.Contains(Search.Text)
- || ((LogEntry)args.Item).Message.Contains(Search.Text);
- };
- DataContext = _filtered;
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- /*_client = new PipeClient<string>("PRSDesktop");
- _client.Connected += _client_Connected;
- _client.ExceptionOccurred += _client_ExceptionOccurred;
- _client.MessageReceived += (o, args) =>
- {
- var m = args.Message;
- var datetime = m.Length > 32 ? m.Substring(0, 12) : string.Format("{0:HH:mm:ss.fff}", DateTime.Now);
- var type = m.Length > 32 ? m.Substring(13, 6) : "INFO";
- var user = m.Length > 32 ? m.Substring(20, 12) : "";
- var msg = m.Length > 32 ? m.Substring(33) : m;
- if (string.Equals(type?.Trim(), "INFO") || string.Equals(type?.Trim(), "ERROR"))
- {
- var logentry = new LogEntry
- {
- DateTime = datetime,
- Type = type,
- User = user,
- Message = msg
- };
- Dispatcher.BeginInvoke((Action)(() => { _logentries.Insert(0, logentry); }));
- }
- };
- _client.ConnectAsync();*/
- logger = new EventLogger(OnLog);
- MainLogger.AddLogger(logger);
- }
- private void OnLog(string message)
- {
- var datetime = message.Length > 32 ? message.Substring(0, 12) : string.Format("{0:HH:mm:ss.fff}", DateTime.Now);
- var type = message.Length > 32 ? message.Substring(13, 6) : "INFO";
- var user = message.Length > 32 ? message.Substring(20, 12) : "";
- var msg = message.Length > 32 ? message.Substring(33) : message;
- if (string.Equals(type.Trim(), "INFO") || string.Equals(type.Trim(), "ERROR"))
- {
- var logentry = new LogEntry
- {
- DateTime = datetime,
- Type = type,
- User = user,
- Message = msg
- };
- Dispatcher.BeginInvoke(() => _logentries.Insert(0, logentry));
- }
- }
- private void Window_Closing(object sender, CancelEventArgs e)
- {
- //_client.DisconnectAsync();
- MainLogger.RemoveLogger(logger);
- }
- private void Search_TextChanged(object sender, TextChangedEventArgs e)
- {
- _filtered.View.Refresh();
- }
- }
- }
|