This repository was archived by the owner on Jul 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogsToEventlog.cs
More file actions
84 lines (73 loc) · 2.77 KB
/
LogsToEventlog.cs
File metadata and controls
84 lines (73 loc) · 2.77 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
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
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace DockerLogToEventLog
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Usage: DockerLogToEventLog.exe <path_to_log_file> <log_name> <source_name>");
return;
}
string logFilePath = args[0];
string logName = args[1];
string sourceName = args[2];
if (!EventLog.SourceExists(sourceName))
{
EventLog.CreateEventSource(sourceName, logName);
}
string[] logLines;
try
{
using (FileStream fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader reader = new StreamReader(fileStream))
{
List<string> lines = new List<string>();
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine());
}
logLines = lines.ToArray();
}
}
catch (IOException e)
{
Console.WriteLine("An IO exception occurred: " + e.Message);
return;
}
catch (Exception e)
{
Console.WriteLine("An exception occurred: " + e.Message);
return;
}
string pattern = @"^\[(?<timestamp>.*?)\]\[(?<source>.*?)\]\[(?<sev_short>.)\] time="".*?"" level=(?<severity>.*?) msg=""(?<message>.*)""";
foreach (var line in logLines)
{
var match = Regex.Match(line, pattern);
if (match.Success)
{
string timestamp = match.Groups["timestamp"].Value;
string source = match.Groups["source"].Value;
string severity = match.Groups["severity"].Value;
string message = match.Groups["message"].Value;
EventLogEntryType entryType = EventLogEntryType.Information;
switch (severity)
{
case "warning":
entryType = EventLogEntryType.Warning;
break;
case "error":
entryType = EventLogEntryType.Error;
break;
}
string finalMessage = $"Timestamp: {timestamp} | Source: {source} | Message: {message}";
EventLog.WriteEntry(sourceName, finalMessage, entryType);
}
}
}
}
}