Console.xaml.cs 5.3 KB

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