-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogReader.cs
83 lines (70 loc) · 2.52 KB
/
LogReader.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using MoreLinq.Extensions;
namespace ArmelloLogTools
{
public class LogReader : IDisposable
{
private StreamReader _reader;
public string Filename { get; private set; }
public LogReader(string filename)
{
Filename = filename;
_reader = new StreamReader(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
}
public void ChangeFile(string filename)
{
Filename = filename;
//Close previous
_reader.Dispose();
//Open new file
_reader = new StreamReader(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
}
public IEnumerable<string> ReadLines()
{
string line;
while ((line = _reader.ReadLine()) != null)
{
yield return line;
}
}
public void Dispose()
{
_reader.Dispose();
}
}
public static class LogFile
{
private static Regex _armelloLogFilenameDatetimeRegex =
new Regex(@"^.+_log_(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}-(?:A|P)M)\.txt$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static readonly string LogDirectoryPath = GetLogDirectoryPath();
public static string LatestLogFile(string dirPath)
{
var dir = new DirectoryInfo(dirPath);
var files = dir.GetFiles("*.txt");
return files.Select(info => (info, match: _armelloLogFilenameDatetimeRegex.Match(info.Name)))
.Where(t => t.match.Success)
.Select(t =>
(t.info,
datetime: DateTime.ParseExact(t.match.Groups[1].Value, "yyyy-MM-dd_hh-mm-ss-tt",
CultureInfo.InvariantCulture)))
.MaxBy(t => t.datetime).First().info.FullName;
}
public static string LatestLogFile()
{
return LatestLogFile(LogDirectoryPath);
}
private static string GetLogDirectoryPath()
{
var relativePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"../LocalLow/League of Geeks/Armello/logs");
var dir = new DirectoryInfo(relativePath);
return dir.FullName;
}
}
}