-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathLogEventViewer.xaml.cs
More file actions
199 lines (170 loc) · 6.96 KB
/
LogEventViewer.xaml.cs
File metadata and controls
199 lines (170 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using NLog;
using Torch.API;
using Torch.Patches;
using MessageBox = System.Windows.Forms.MessageBox;
namespace Torch.Server.Views
{
public class LogViewModel
{
public ObservableCollection<LogEvent> LogEvents { get; set; }
public LogViewModel()
{
LogEvents = new ObservableCollection<LogEvent>();
// Optionally, populate your collection with initial data here
}
}
public class LogEvent
{
public string Time { get; set; }
public string Level { get; set; }
public string Class { get; set; }
public string Message { get; set; }
}
public partial class LogEventViewer : UserControl
{
private static Logger _log = LogManager.GetCurrentClassLogger();
private ObservableCollection<LogEvent> Events = new ObservableCollection<LogEvent>();
string[] logLevels =
{
"No filter",
"Warn",
"Error",
"Fatal"
};
string[] classFilters =
{
"No filter",
};
public LogEventViewer()
{
InitializeComponent();
//array of filterable log levels
//populate the log level filter combo box
LevelFilterComboBox.ItemsSource = logLevels;
ClassFilterComboBox.ItemsSource = classFilters;
DataContext = new LogViewModel();
NlogCustomTarget.LogEventReceived += LogEvent;
TorchServer.Instance.SessionUnloading += Unloading;
//on loaded event
Loaded += (sender, args) =>
{
//set the log level filter to the first item in the combo box
LevelFilterComboBox.SelectedIndex = 0;
ClassFilterComboBox.SelectedIndex = 0;
if (TorchServer.Instance.Config.BranchName == TorchBranchType.dev)
{
TestButton.Visibility = Visibility.Visible;
}
};
}
private void Unloading()
{
NlogCustomTarget.LogEventReceived -= LogEvent;
}
private void LogEvent(LogEventInfo obj)
{
// Use Dispatcher.Invoke to ensure that the following code block is executed on the UI thread.
Dispatcher.BeginInvoke(() =>
{
try
{
// Check if the class (LoggerName) is already in the list to avoid InvalidOperationException
if (!classFilters.Any(c => string.Equals(c, obj.LoggerName, StringComparison.OrdinalIgnoreCase)))
{
// It's safe to manipulate the UI here since we're on the UI thread.
var updatedClassFilters = classFilters.Append(obj.LoggerName).ToArray();
ClassFilterComboBox.ItemsSource = updatedClassFilters;
classFilters = updatedClassFilters; // Ensure classFilters is updated for future checks.
}
AddRow(obj.Level.ToString(), obj.LoggerName, (obj.Message == "{0}" ? obj.FormattedMessage : obj.Message));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}, DispatcherPriority.Background);
}
private void LevelFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplyFilters();
}
private void ClassFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplyFilters();
}
private void ResetFiltersButton_Click(object sender, RoutedEventArgs e)
{
LevelFilterComboBox.SelectedIndex = 0; // Reset to "No filter"
ClassFilterComboBox.SelectedIndex = 0; // Reset to "No filter"
ApplyFilters();
}
private void ApplyFilters()
{
try
{
string selectedLevel = LevelFilterComboBox.SelectedItem as string;
string selectedClass = ClassFilterComboBox.SelectedItem as string;
// Use LINQ to filter LogEvents based on the selected criteria
var filtered = ((LogViewModel)DataContext).LogEvents.Where(logEvent =>
{
bool levelMatch = (selectedLevel == "No filter" && logEvent.Level != "Warn") || // Exclude "Warn" unless explicitly selected
logEvent.Level.Equals(selectedLevel, StringComparison.OrdinalIgnoreCase);
bool classMatch = selectedClass == "No filter" ||
logEvent.Class.Equals(selectedClass, StringComparison.OrdinalIgnoreCase);
return levelMatch && classMatch;
}).ToList();
// Update the DataGrid's ItemsSource with the filtered list
LogEventViewerDataGrid.ItemsSource = new ObservableCollection<LogEvent>(filtered);
LogEventViewerDataGrid.Items.Refresh();
}
catch (Exception e)
{
// Consider logging the exception or handling it as needed
MessageBox.Show($"Error applying filters: {e.Message}", "Filter Error");
}
}
private void LogEventViewerDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (sender is DataGrid grid && grid.SelectedItem is LogEvent logEvent)
{
var messageWindow = new LogMessageWindow
{
Owner = Window.GetWindow(this) // Set owner if you want
};
messageWindow.MessageTextBox.Text = logEvent.Message;
messageWindow.ShowDialog(); // Show the window as a dialog
}
}
//method to add row to datagrid
private void AddRow(string level, string caller, string message)
{
var logEvent = new LogEvent()
{
Time = DateTime.Now.TimeOfDay.ToString("hh\\:mm\\:ss"),
Level = level,
Class = caller,
Message = message
};
Events.Add(logEvent);
//add to datagrid in reverse order
((LogViewModel) DataContext).LogEvents.Insert(0, logEvent);
ApplyFilters();
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
((LogViewModel) DataContext).LogEvents.Clear();
}
private void TestButton_OnClickButton_Click(object sender, RoutedEventArgs e)
{
_log.Error("Test error message");
}
}
}