I have a need to read different kinds of log files and extract relevant information (timestamp, hostname, message etc.) in .NET. Is there a supported .NET standard library that support parsing both the BSD syslog (RFC3164) and IETF syslog (RFC5424) out there?
Asked
Active
Viewed 654 times
1 Answers
1
I had the same issue and unfortunately I could not find anything suitable so I ended up writing my own code.
For RFC5424 this worked for me: Regular Expression for SysLog RFC5424 For RFC3164 you could adapt the code like so (quick and dirty version):
private const string PrivalPattern = @"\<(?<PRIVAL>\d{1,3})\>";
private const string TimestampPattern = @"(?<TIMESTAMP>(?:(?:\w)+ (?:(?:0?[1-9]|[12][0-9]|3[01)])) (?:0?[0-9]|1[0-9]|2[0-3]):(?:0?[0-9]|[1-5][0-9]):(?:0?[0-9]|[1-5][0-9])))";
private const string HostnamePattern = @"(?<HOSTNAME>(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:\.|\S|\w)+)";
private const string MessagePattern = @"(?<MESSAGE>.+)?";
new Regex($@"^{PrivalPattern}{TimestampPattern} {HostnamePattern} {MessagePattern}",
RegexOptions.None,
new TimeSpan(0, 0, 5));

Dharman
- 30,962
- 25
- 85
- 135

Yamamotooko
- 96
- 1
- 8