-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (36 loc) · 1.31 KB
/
Program.cs
File metadata and controls
46 lines (36 loc) · 1.31 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
using System.Globalization;
using System.Text.RegularExpressions;
namespace MergeLogs;
public class Program
{
static void Main(string[] args)
{
var directoryPath = string.Empty;
if (args.Count() == 0 || string.IsNullOrEmpty(args[0]))
{
Console.WriteLine("Enter log root directory path:");
var input = Console.ReadLine();
while (!Directory.Exists(input))
{
Console.WriteLine("Directory does not exsist\n\tTry again:");
input = Console.ReadLine();
}
directoryPath = input;
}
List<string> matchedLines = new List<string>();
string[] logFiles = Directory.GetFiles(directoryPath, "*.log", SearchOption.AllDirectories);
var regex = new Regex(@"^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d{3})");
foreach (string file in logFiles)
{
var lines =
File.ReadLines(file)
.Where(line => regex.IsMatch(line))
.ToList();
matchedLines.AddRange(lines);
}
matchedLines.Sort();
matchedLines.ForEach(Console.WriteLine);
string outputFile = Path.Combine(directoryPath, "merged.log");
File.WriteAllLines(outputFile, matchedLines);
}
}