-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
294 lines (233 loc) · 9.6 KB
/
MainWindow.xaml.cs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using LogViewerAppWPF;
using Microsoft.Win32;
namespace LogViewerApp {
public partial class MainWindow : Window {
private List<LogEntry> logs = new List<LogEntry>();
private List<LogEntry> filteredLogs = new List<LogEntry>();
private List<LogEntry> paginatedLogs = new();
private bool isExpanded = true;
private int logCount;
private int warningCount;
private int errorCount;
private int currentPage = 1;
private int totalPages;
public event PropertyChangedEventHandler PropertyChanged;
//public string PageInfo;//=> $"Page {currentPage} of {totalPages}";
private int PageSize = 10; // Number of items per page
public MainWindow() {
InitializeComponent();
}
private void LoadFileButton_Click(object sender, RoutedEventArgs e) {
PageSizeComboBox.SelectedIndex = 0;
OpenFileDialog fileDialog = new() {
Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
};
if (fileDialog.ShowDialog() != true)
return;
logs = ParseLogs(File.ReadAllText(fileDialog.FileName));
logs.ForEach(log => {
if (log.LogLevel != "Exception") {
log.StackTraceFull = Utilites.GetTruncatedStackTrace(log.StackTraceFull);
log.StackTrace = log.StackTrace1stLine = log.StackTraceFull.Split('\n')[0];
}
(log.ClassTraceFull, log.ClassTrace1st) = Utilites.ExtractClassNamesAndLineNumbers(log.StackTraceFull);
log.ClassTrace = log.ClassTrace1st;
//DateTime myDate = DateTime.Parse(log.Timestamp, System.Globalization.CultureInfo.InvariantCulture);
//log.Timestamp = myDate.ToString("dd/MM/yyyy HH:mm:ss");
});
if (PageSizeComboBox.Items.Count <= 0) {
for (int i = 0; i < pagesizeArray.Length; i++) {
PageSizeComboBox.Items.Insert(i, pagesizeArray[i]);
}
}
UpdateDisplay();
}
private List<LogEntry> ParseLogs(string logText) {
if ((bool)Encrypted.IsChecked) {
logText = EncryptDecrypt.Decrypt(logText);
}
Regex logEntryPattern = new(
@"(?<TimeStamp>\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}) - (?<LogLevel>\w+)\r?\n(?<Message>.*?)\r?\n(?<StackTrace>(?:.|\r?\n)+?)(?=\r?\n\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2} - \w+|$)",
RegexOptions.Singleline);
return logEntryPattern.Matches(logText).Cast<Match>().Select(match => new LogEntry {
Timestamp = match.Groups["TimeStamp"].Value,
LogLevel = match.Groups["LogLevel"].Value,
MassageFull = match.Groups["Message"].Value.Trim(),
StackTraceFull = match.Groups["StackTrace"].Value.Trim(),
Message = match.Groups["Message"].Value.Split('\n')[0],
Massage1stLine = match.Groups["Message"].Value.Split('\n')[0],
StackTrace = match.Groups["StackTrace"].Value.Split('\n')[0]
}).ToList();
}
private void UpdateDisplay() {
filteredLogs = logs.Where(FilterLog).ToList();
UpdatePagination();
if (CollapseFilter.IsChecked == true) {
paginatedLogs = paginatedLogs
.GroupBy(log => new { log.Message, log.LogLevel })
.Select(group => group.First())
.ToList();
}
logCount = 0;
warningCount = 0;
errorCount = 0;
foreach (var item in paginatedLogs) {
if (item.LogLevel == "Log") {
logCount += 1;
} else if (item.LogLevel == "Warning") {
warningCount += 1;
} else {
errorCount += 1;
}
}
LogFilter.Content = $"Log ({logCount})";
WarningFilter.Content = $"Warning ({warningCount})";
ErrorFilter.Content = $"Error ({errorCount})";
}
private bool FilterLog(LogEntry log) {
// Combine search, log level, and date range filters
var searchTerm = SearchBox.Text.ToLower();
if (!string.IsNullOrEmpty(searchTerm) &&
!log.Message.Contains(searchTerm) &&
!log.StackTrace.Contains(searchTerm)) {
return false;
}
return FilterByLogLevel(log) && FilterByDate(log);
}
private bool FilterByLogLevel(LogEntry log) {
return (LogFilter.IsChecked == true || log.LogLevel != "Log") &&
(WarningFilter.IsChecked == true || log.LogLevel != "Warning") &&
(ErrorFilter.IsChecked == true || log.LogLevel != "Error");
}
private bool FilterByDate(LogEntry log) {
DateTime logDate = DateTime.Parse(log.Timestamp);
// Check Start Date
if (StartDatePicker.SelectedDate.HasValue && logDate < StartDatePicker.SelectedDate.Value)
return false;
// Check End Date
if (EndDatePicker.SelectedDate.HasValue && logDate > EndDatePicker.SelectedDate.Value)
return false;
return true;
}
private void ExpandAllButton_Click(object sender, RoutedEventArgs e) {
isExpanded = !isExpanded;
paginatedLogs.ForEach(log => log.IsExpanded = isExpanded);
foreach (var item in paginatedLogs) {
logListView.SelectedItem = item;
LogListView_MouseDoubleClick(default, default);
}
if (!isExpanded) {
ExpandAllButton.Content = "Colleps All";
} else {
ExpandAllButton.Content = "Expand All";
}
}
private void SearchButton_Click(object sender, RoutedEventArgs e) => UpdateDisplay();
private void FilterButton_Click(object sender, RoutedEventArgs e) => UpdateDisplay();
private void LogListView_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
if (logListView.SelectedItem is not LogEntry log)
return;
log.IsExpanded = !log.IsExpanded;
log.StackTrace = log.IsExpanded ? log.StackTraceFull : log.StackTrace1stLine;
log.Message = log.IsExpanded ? log.MassageFull : log.Massage1stLine;
log.ClassTrace = log.IsExpanded ? log.ClassTraceFull : log.ClassTrace1st;
logListView.Items.Refresh();
}
private void UpdatePagination() {
totalPages = (int)Math.Ceiling((double)filteredLogs.Count / PageSize);
// Get the items for the current page
paginatedLogs = filteredLogs.Skip((currentPage - 1) * PageSize).Take(PageSize).ToList();
// Bind the current page's items to the ListView
logListView.ItemsSource = paginatedLogs;
PageInfo.Text = $"Page {currentPage} of {totalPages}";
// Notify the UI of property changes
}
private void PreviousPage_Click(object sender, RoutedEventArgs e) {
if (currentPage > 1) {
currentPage--;
UpdatePagination();
}
}
private void NextPage_Click(object sender, RoutedEventArgs e) {
if (currentPage < totalPages) {
currentPage++;
UpdatePagination();
}
}
private void PageSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
PageSize = pagesizeArray[PageSizeComboBox.SelectedIndex];
UpdateDisplay();
}
//private void ThemeToggleButton_Checked(object sender, RoutedEventArgs e) {
// SetNightMode();
// ThemeToggleButton.Content = "☀️ Day Mode";
//}
//private void ThemeToggleButton_Unchecked(object sender, RoutedEventArgs e) {
// SetDayMode();
// ThemeToggleButton.Content = "🌙 Night Mode";
//}
private void SetNightMode() {
this.Background = Brushes.Black;
this.Foreground = Brushes.White;
}
private void SetDayMode() {
this.Background = Brushes.White;
this.Foreground = Brushes.Black;
}
private int[] pagesizeArray = new int[] {
10,25,50,100
};
}
public class LogEntry {
public string Timestamp {
get; set;
}
public string LogLevel {
get; set;
}
public string Message {
get; set;
}
public string Massage1stLine {
get; set;
}
public string MassageFull {
get; set;
}
public string StackTrace {
get; set;
}
public string StackTrace1stLine {
get; set;
}
public string StackTraceFull {
get; set;
}
public string ClassTrace {
get; set;
}
public string ClassTrace1st {
get; set;
}
public string ClassTraceFull {
get; set;
}
public bool IsExpanded {
get; set;
}
public string MethodTrace {
get; set;
}
}
}