-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogReader.cs
223 lines (194 loc) · 7.69 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Model
{
public class LogReader
{
const char LeftBrace = '{';
const char RightBrace = '}';
readonly HashSet<string> IgnoredTypes;
AppSettings Settings { get; set; }
LogEntryTransferManager Manager { get; set; }
string[] Files = new string[] { };
int CurrFileIndex = -1;
public int FileCount => Files.Length;
public bool FilesFound => Files.Length > 0;
public LogReader(AppSettings settings, LogEntryTransferManager manager)
{
this.Settings = settings;
this.Manager = manager;
this.IgnoredTypes = new HashSet<string>(settings.IgnoredTypes);
GetFiles();
}
public bool Read()
{
CurrFileIndex++;
if (CurrFileIndex <= Files.Length - 1)
{
return true;
}
return false;
}
public async Task Process()
{
var curr = Files[CurrFileIndex];
var braceCount = 0;
var chars = new List<char>();
char ch;
using (StreamReader sr = new StreamReader(curr))
{
while (!sr.EndOfStream)
{
ch = (char)sr.Read();
chars.Add(ch);
if (ch == LeftBrace)
{
braceCount++;
}
else if (ch == RightBrace)
{
braceCount--;
if (braceCount == 0)
{
var json = new string(chars.ToArray());
try
{
var record = JsonConvert.DeserializeObject<LogRecord>(json);
var entry = record.ToLogEntry();
if (IgnoredTypes.Contains(entry.MessageTemplate))
{
chars.Clear();
continue;
}
Manager.Add(entry);
if (Manager.RowCount == Settings.BatchSize)
{
await Manager.ToSql();
}
}
catch (Exception ex)
{
Console.WriteLine($"Unparsable log entry found. JSON: {json}. Error: {ex.Message}");
}
chars.Clear();
}
}
}
if (Manager.RowCount > 0)
{
await Manager.ToSql();
}
}
if (!Settings.PreventArchive)
{
var outpath = $"{Settings.OutputDirPath}{Path.DirectorySeparatorChar}{Path.GetFileName(curr)}";
File.Move(curr, outpath);
}
Console.WriteLine($"Completed {curr}...");
}
void GetFiles()
{
var ext = "log";
var todayStr = DateTime.Now.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
var datadir = Directory.GetFiles(Settings.SourceDirPath).Select(p => Path.GetFileName(p));
var archived = Directory.GetFiles(Settings.OutputDirPath).Select(p => Path.GetFileName(p));
// If copy all remote files found
if (!string.IsNullOrEmpty(Settings.CopyAllDirPath))
{
var toCopy = Directory.GetFiles(Settings.CopyAllDirPath)
.Where(f => f.EndsWith($".{ext}"))
.Where(f => !archived.Contains(Path.GetFileName(f)))
.Where(f => !datadir.Contains(Path.GetFileName(f)))
.ToArray();
if (Settings.IgnoreCurrent)
{
toCopy = toCopy.Where(f => !f.Contains(todayStr)).ToArray();
}
foreach (var file in toCopy)
{
try
{
var outfile = Path.Combine(Settings.SourceDirPath, Path.GetFileName(file));
File.Copy(file, Path.Combine(file, outfile), true);
}
catch (Exception ex)
{
Console.WriteLine($"Error copying remote file: {ex.Message}");
}
}
}
// If copy only latest file
if (!string.IsNullOrEmpty(Settings.CopyLatestDirPath))
{
var toCopy = "";
if (Settings.IgnoreCurrent)
{
toCopy = Directory.GetFiles(Settings.CopyLatestDirPath)
.Where(f => f.EndsWith($".{ext}"))
.Where(f => !archived.Contains(Path.GetFileName(f)))
.Where(f => !datadir.Contains(Path.GetFileName(f)))
.Where(f => !f.Contains(todayStr))
.OrderByDescending(f => f)
.FirstOrDefault();
}
else
{
toCopy = Directory.GetFiles(Settings.CopyLatestDirPath)
.Where(f => f.EndsWith($".{ext}"))
.Where(f => !archived.Contains(Path.GetFileName(f)))
.Where(f => !datadir.Contains(Path.GetFileName(f)))
.OrderByDescending(f => f)
.FirstOrDefault();
}
if (toCopy != null)
{
try
{
var outfile = Path.Combine(Settings.SourceDirPath, Path.GetFileName(toCopy));
File.Copy(toCopy, Path.Combine(toCopy, outfile), true);
}
catch (Exception ex)
{
Console.WriteLine($"Error copying remote file: {ex.Message}");
}
}
}
// If copy a specific file
if (!string.IsNullOrWhiteSpace(Settings.SpecificFile))
{
var path = Path.Combine(Settings.SourceDirPath, Settings.SpecificFile);
if (!File.Exists(path))
{
Console.WriteLine($"The specified file '{Settings.SpecificFile}' could not be found.");
return;
}
Files = new string[] { path };
return;
}
// Redundant check but to be safe, delete any files already archived but mistakenly copied over again
var pulled = Directory.GetFiles(Settings.SourceDirPath).Select(p => Path.GetFileName(p));
foreach (var file in archived.Intersect(pulled))
{
try
{
Directory.Delete(Path.Combine(Settings.SourceDirPath, file));
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file: {ex.Message}");
}
}
Files = Directory.GetFiles(Settings.SourceDirPath)
.Where(f => f.EndsWith($".{ext}"))
.ToArray();
if (Settings.IgnoreCurrent)
{
Files = Files.Where(f => !f.Contains(todayStr)).ToArray();
}
}
}
}