Console.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using H.Pipes;
  9. using InABox.Logging;
  10. using InABox.Wpf;
  11. namespace PRSDesktop
  12. {
  13. public class PropertyChangedBase : INotifyPropertyChanged
  14. {
  15. public event PropertyChangedEventHandler? PropertyChanged;
  16. protected virtual void OnPropertyChanged(string propertyName)
  17. {
  18. Application.Current.Dispatcher.BeginInvoke((Action)(() =>
  19. {
  20. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  21. }));
  22. }
  23. }
  24. public class LogEntry : PropertyChangedBase
  25. {
  26. public string DateTime { get; set; } = "";
  27. public string Type { get; set; } = "";
  28. public string User { get; set; } = "";
  29. public string Message { get; set; } = "";
  30. }
  31. public class CollapsibleLogEntry : LogEntry
  32. {
  33. public List<LogEntry> Contents { get; set; }
  34. }
  35. public static class ItemsControlExtensions
  36. {
  37. public static void ScrollIntoView(
  38. this ItemsControl control,
  39. object item)
  40. {
  41. var framework =
  42. control.ItemContainerGenerator.ContainerFromItem(item)
  43. as FrameworkElement;
  44. if (framework == null) return;
  45. framework.BringIntoView();
  46. }
  47. public static void ScrollIntoView(this ItemsControl control)
  48. {
  49. var count = control.Items.Count;
  50. if (count == 0) return;
  51. var item = control.Items[count - 1];
  52. control.ScrollIntoView(item);
  53. }
  54. }
  55. /// <summary>
  56. /// Interaction logic for Console.xaml
  57. /// </summary>
  58. public partial class Console : ThemableWindow
  59. {
  60. //private PipeClient<string> _client;
  61. public CollectionViewSource _filtered;
  62. private readonly ObservableCollection<LogEntry> _logentries;
  63. private EventLogger logger;
  64. public Console()
  65. {
  66. InitializeComponent();
  67. _logentries = new ObservableCollection<LogEntry>();
  68. _filtered = new CollectionViewSource();
  69. _filtered.Source = _logentries;
  70. _filtered.Filter += (sender, args) =>
  71. {
  72. args.Accepted = string.IsNullOrWhiteSpace(Search.Text)
  73. || ((LogEntry)args.Item).DateTime.Contains(Search.Text)
  74. || ((LogEntry)args.Item).Type.Contains(Search.Text)
  75. || ((LogEntry)args.Item).User.Contains(Search.Text)
  76. || ((LogEntry)args.Item).Message.Contains(Search.Text);
  77. };
  78. DataContext = _filtered;
  79. }
  80. private void Window_Loaded(object sender, RoutedEventArgs e)
  81. {
  82. /*_client = new PipeClient<string>("PRSDesktop");
  83. _client.Connected += _client_Connected;
  84. _client.ExceptionOccurred += _client_ExceptionOccurred;
  85. _client.MessageReceived += (o, args) =>
  86. {
  87. var m = args.Message;
  88. var datetime = m.Length > 32 ? m.Substring(0, 12) : string.Format("{0:HH:mm:ss.fff}", DateTime.Now);
  89. var type = m.Length > 32 ? m.Substring(13, 6) : "INFO";
  90. var user = m.Length > 32 ? m.Substring(20, 12) : "";
  91. var msg = m.Length > 32 ? m.Substring(33) : m;
  92. if (string.Equals(type?.Trim(), "INFO") || string.Equals(type?.Trim(), "ERROR"))
  93. {
  94. var logentry = new LogEntry
  95. {
  96. DateTime = datetime,
  97. Type = type,
  98. User = user,
  99. Message = msg
  100. };
  101. Dispatcher.BeginInvoke((Action)(() => { _logentries.Insert(0, logentry); }));
  102. }
  103. };
  104. _client.ConnectAsync();*/
  105. logger = new EventLogger(OnLog);
  106. MainLogger.AddLogger(logger);
  107. }
  108. private void OnLog(string message)
  109. {
  110. var datetime = message.Length > 32 ? message.Substring(0, 12) : string.Format("{0:HH:mm:ss.fff}", DateTime.Now);
  111. var type = message.Length > 32 ? message.Substring(13, 6) : "INFO";
  112. var user = message.Length > 32 ? message.Substring(20, 12) : "";
  113. var msg = message.Length > 32 ? message.Substring(33) : message;
  114. if (string.Equals(type.Trim(), "INFO") || string.Equals(type.Trim(), "ERROR"))
  115. {
  116. var logentry = new LogEntry
  117. {
  118. DateTime = datetime,
  119. Type = type,
  120. User = user,
  121. Message = msg
  122. };
  123. Dispatcher.BeginInvoke(() => _logentries.Insert(0, logentry));
  124. }
  125. }
  126. private void Window_Closing(object sender, CancelEventArgs e)
  127. {
  128. //_client.DisconnectAsync();
  129. MainLogger.RemoveLogger(logger);
  130. }
  131. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  132. {
  133. _filtered.View.Refresh();
  134. }
  135. }
  136. }