public void TestSQLLog() { string log = @"2019-02-13 04:50:33.89 Server Microsoft SQL Server 2014 (SP2-GDR) (KB4057120) - 12.0.5214.6 (X64) Jan 9 2018 15:03:12 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor) 2019-02-13 04:50:34.00 Server UTC adjustment: 0:00"; using (Stream stream = Utility.StringToStream(log)) using (StreamReader sr = new StreamReader(stream)) { string extractionPatterm = "^\\s*(?<TimeStamp>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{2})\\s(?<Source>\\w+)\\s+(?<Message>.*)$"; TimeStampRecordParser parser = new TimeStampRecordParser( "yyyy-MM-dd HH:mm:ss.ff", null, extractionPatterm, "Singleline", DateTimeKind.Utc, new RegexRecordParserOptions() ); var records = parser.ParseRecords(sr, new LogContext()).ToList(); Assert.Equal(2, records.Count); Assert.Equal("2019-02-13 04:50:33.89", records[0].Data["TimeStamp"]); Assert.Equal("Server", records[0].Data["Source"]); Assert.True(records[0].Data["Message"].Length > 0); } }
private static void TestTimestampLogInternal(DateTime expectedTime1, DateTime expectedTime2, DateTimeKind timeZoneKind) { StringBuilder sb = new StringBuilder(); sb.AppendLine("2017-05-18 00:00:28.0665 Quartz.Core.QuartzSchedulerThread DEBUG Batch acquisition of 0 triggers"); sb.Append("2017-05-18 00:00:56.8128 Quartz.Core.QuartzSchedulerThread DEBUG Batch acquisition of 0 triggers"); string log = sb.ToString(); using (Stream stream = Utility.StringToStream(log)) using (StreamReader sr = new StreamReader(stream)) { TimeStampRecordParser parser = new TimeStampRecordParser("yyyy-MM-dd HH:mm:ss.ffff", null, timeZoneKind); var records = parser.ParseRecords(sr, new LogContext()).ToList(); Assert.Equal("2017-05-18 00:00:28.0665 Quartz.Core.QuartzSchedulerThread DEBUG Batch acquisition of 0 triggers", records[0].GetMessage(null)); Assert.Equal(expectedTime1, records[0].Timestamp); Assert.Equal("2017-05-18 00:00:56.8128 Quartz.Core.QuartzSchedulerThread DEBUG Batch acquisition of 0 triggers", records[1].GetMessage(null)); Assert.Equal(expectedTime2, records[1].Timestamp); } }
public void TestSSMLog() { StringBuilder sb = new StringBuilder(); sb.AppendLine("2017-03-31 10:06:20 ERROR [instanceID=i-0ad46e850f00b20ba] [MessageProcessor] error when calling AWS APIs. error details - GetMessages Error: AccessDeniedException: User: arn:aws:sts::266928793956:assumed-role/ds2amazonli/i-0ad46e850f00b20ba is not authorized to perform: ec2messages:GetMessages on resource: *"); sb.AppendLine("status code: 400, request id: afe4d9d7-15f9-11e7-8eb7-193e481a50d1"); sb.AppendLine("2017-03-31 10:06:21 INFO [instanceID=i-0ad46e850f00b20ba] [MessageProcessor] increasing error count by 1"); string log = sb.ToString(); using (Stream stream = Utility.StringToStream(log)) using (StreamReader sr = new StreamReader(stream)) { TimeStampRecordParser parser = new TimeStampRecordParser("yyyy-MM-dd HH:mm:ss", null, DateTimeKind.Utc); var records = parser.ParseRecords(sr, new LogContext()).ToList(); Assert.Equal("2017-03-31 10:06:20 ERROR [instanceID=i-0ad46e850f00b20ba] [MessageProcessor] error when calling AWS APIs. error details - GetMessages Error: AccessDeniedException: User: arn:aws:sts::266928793956:assumed-role/ds2amazonli/i-0ad46e850f00b20ba is not authorized to perform: ec2messages:GetMessages on resource: *" + Environment.NewLine + "status code: 400, request id: afe4d9d7-15f9-11e7-8eb7-193e481a50d1", records[0].GetMessage(null)); Assert.Equal(new DateTime(2017, 3, 31, 10, 6, 20), records[0].Timestamp); Assert.Equal("2017-03-31 10:06:21 INFO [instanceID=i-0ad46e850f00b20ba] [MessageProcessor] increasing error count by 1", records[1].GetMessage(null)); Assert.Equal(new DateTime(2017, 3, 31, 10, 6, 21), records[1].Timestamp); } }
private bool ValidateTimeStamp(string directory, string logName, IConfigurationRoot config, IConfigurationSection sourceSection, string curId, IList <String> messages) { string log = GetLog(directory, logName).ToString(); using (Stream stream = Utility.StringToStream(log)) using (StreamReader sr = new StreamReader(stream)) { string timestampFormat = config[$"{sourceSection.Path}:{"TimestampFormat"}"]; TimeStampRecordParser parser = new TimeStampRecordParser(timestampFormat, null, DateTimeKind.Utc); var records = parser.ParseRecords(sr, new LogContext()).ToList(); if (records.Count == 1) { messages.Add("Invalid Timestamp format at source ID: " + curId); return(false); } else { messages.Add("Valid Timestamp format at source ID: " + curId); return(true); } } }