This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogFileProcessor.cs
217 lines (192 loc) · 8.57 KB
/
LogFileProcessor.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
using System;
using System.Globalization;
using System.IO;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using EFCore.BulkExtensions;
using LogParserApp.Entities;
using LogParserApp.Utilities;
using static LogParserApp.Utilities.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using LogParserApp.Data;
namespace LogParserApp
{
public partial class LogFileProcessor(LogDbContext dbContext, ILogger<LogFileProcessor> logger)
{
private readonly LogDbContext _dbContext = dbContext;
private readonly ILogger<LogFileProcessor> _logger = logger;
public async Task<bool> ProcessLogFileAsync(string filePath)
{
bool result = true;
List<LogEntry> logEntries = [];
string[] lines = [];
string fileName = string.Empty;
string fileNameNoExt;
string? folderName = string.Empty;
DateTime fileDate = new();
string fileHash = string.Empty;
int logFileId = 0;
string fileType = string.Empty;
List<ParsingError> errors = [];
try
{
fileName = Path.GetFileName(filePath);
fileNameNoExt = Path.GetFileNameWithoutExtension(filePath);
folderName = Path.GetDirectoryName(filePath);
fileDate = File.GetLastWriteTime(filePath);
fileHash = await Hashing.ComputeSha256HashAsync(filePath);
logFileId = ExtractLogFileId(fileNameNoExt);
fileType = ExtractFileType(fileNameNoExt);
lines = await File.ReadAllLinesAsync(filePath);
}
catch (FileNotFoundException ex)
{
_logger.LogError("File not found: {ex.FileName} {ex.Message}", ex.FileName, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
catch (DirectoryNotFoundException ex)
{
_logger.LogError("Directory not found: {folderName} {ex.Message}", folderName, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
catch (SecurityException ex)
{
_logger.LogError("Access denied: {filePath} {ex.Message}", filePath, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
catch (IOException ex)
{
_logger.LogError("I/O Error: {filePath} {ex.Message}", filePath, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
catch (UnauthorizedAccessException ex)
{
_logger.LogError("Access denied: {filePath} {ex.Message}", filePath, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
if (await LogAlreadyProcessedAsync(fileName))
{
_logger.LogInformation("Log file already processed: {fileName}", fileName);
result = false;
}
if (!result) return false;
using var transaction = await _dbContext.Database.BeginTransactionAsync();
try
{
var parsedLog = new ParsedLog
{
FileName = fileName!,
LogType = fileType,
LogFileId = logFileId,
DateParsed = DateTime.UtcNow,
FileDate = fileDate,
FileHash = fileHash
};
await _dbContext.ParsedLogs.AddAsync(parsedLog);
await _dbContext.SaveChangesAsync();
int parsedLogId = parsedLog.Id;
int lineNum = 0;
foreach (var line in lines)
{
var entry = ParseLine(line, parsedLogId, lineNum);
if (entry != null)
{
logEntries.Add(entry);
}
else
{
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = $"Unable to parse or convert line {lineNum}" });
}
lineNum += 1;
}
await _dbContext.BulkInsertAsync(logEntries);
await _dbContext.SaveChangesAsync();
await transaction.CommitAsync();
_logger.LogInformation("Log file: {fileName} processed and data committed to the database.", fileName);
result = true;
}
catch (Exception ex)
{
await transaction.RollbackAsync();
_logger.LogError("Error processing log file: {fileName} {ex.Message}", fileName, ex.Message);
errors.Add(new ParsingError { FileName = fileName ?? "", ErrorMessage = ex.Message.FormatExceptionMessageForDb() });
result = false;
}
if (!result || errors.Count > 1)
{
await _dbContext.ParsingErrors.AddRangeAsync(errors);
await _dbContext.SaveChangesAsync();
};
return result;
}
private async Task<bool> LogAlreadyProcessedAsync(string? fileName)
{
return fileName != null && await _dbContext.ParsedLogs.AsNoTracking().AnyAsync(l => l.FileName == fileName);
}
private static string ExtractFileType(string fileNameNoExt)
{
var match = FileTypeRegex().Match(fileNameNoExt);
return match.Success ? match.Groups[1].Value : "unknown";
}
private static int ExtractLogFileId(string fileNameNoExt)
{
var match = FileIdRegex().Match(fileNameNoExt);
return match.Success ? int.Parse(match.Groups[1].Value) : 0;
}
private static LogEntry? ParseLine(string line, int parsedLogId, int lineNum)
{
try
{
var parts = line.RemoveNullChars().Split("->", StringSplitOptions.TrimEntries); //null characters "\0" cause problems
if (parts.Length < 2) return null;
var dateTimePart = parts[0].Trim();
string ipPart;
string statusAndRestPart;
// Check if the IP address field present (some logs do not include IP address)
if (parts.Length == 3)
{
ipPart = parts[1].Trim();
statusAndRestPart = parts[2].Trim();
}
else
{
ipPart = string.Empty;
statusAndRestPart = parts[1].Trim();
}
var statusPart = statusAndRestPart.Split(':', StringSplitOptions.TrimEntries)[0];
var actionDetailsPart = ActionDetailsRegex().Match(statusAndRestPart);
string action = actionDetailsPart.Groups[1].Value.Trim();
string details = actionDetailsPart.Groups.Count > 2 ? actionDetailsPart.Groups[2].Value.Trim() : string.Empty;
return new LogEntry
{
ParsedLogId = parsedLogId,
LineNum = lineNum,
EntryDate = DateTime.ParseExact(dateTimePart, "ddd, dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture),
IPaddress = ipPart,
Status = statusPart,
Action = action,
Details = details
};
}
catch (Exception ex)
{
throw new Exception($"Error processing line# {lineNum} {ex.Message}");
}
}
// generates all regexes at compile time
[GeneratedRegex(@"^(.*?)_\d+$")]
private static partial Regex FileTypeRegex();
[GeneratedRegex(@"_([0-9]+)$")]
private static partial Regex FileIdRegex();
[GeneratedRegex(@"Action=\[(.*?)\](?:, Details=\[(.*?)\])?", RegexOptions.Compiled)]
private static partial Regex ActionDetailsRegex();
}
}