public void ParseFile(MoniterSetting moniter) { //https://dotnet-snippets.de/snippet/apache-log-file-parsen-regex/5969 //24.236.252.67 - - [17/May/2015:10:05:40 +0000] "GET /favicon.ico HTTP/1.1" 200 3638 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0" string logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+|-) \"([^\"]+)\" \"([^\"]+)\""; SubtotalItem subtotal = new SubtotalItem(moniter.ReportServerName); foreach (string path in moniter.LogPath()) { using (StreamReader sr = File.OpenText(path)) { string s = String.Empty; while ((s = sr.ReadLine()) != null) { //we're just testing read speeds Match regexMatch = Regex.Match(s, logEntryPattern); DateTime time; if (!DateTime.TryParseExact(regexMatch.Groups[4].Value, "dd/MMM/yyyy:HH:mm:ss zzz", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out time)) { continue; } string Request = regexMatch.Groups[5].Value; string Response = regexMatch.Groups[6].Value; int BytesSent = 0; if (!int.TryParse(regexMatch.Groups[7].Value, out BytesSent)) { BytesSent = 0; } string apiName = "Others"; if (Request == "GET /robots.txt HTTP/1.1") { apiName = "robots"; } if (Request == "GET /projects/xdotool/ HTTP/1.1") { apiName = "xdotool"; } if (BytesSent >= 30000) { subtotal.OverStatistics(apiName, Response); } else { subtotal.Statistics(apiName, Response); } } } } SummaryList.Add(subtotal); }
private StringBuilder SummaryToTable() { StringBuilder sb = new StringBuilder(); sb.AppendLine("<table width='100%' border='1' cellpadding='0' cellspacing='0' style='border:2px #26FF26 solid;text-align:center;'>"); SubtotalItem TotalItem = new SubtotalItem("總計"); sb.Append("<tr><th rowspan='2'>Server</th>"); foreach (var item in TotalItem.SummaryData.Keys) { sb.AppendFormat("<th colspan='4'>{0}</th>", item); } sb.Append("<th colspan='2'>小計</th></tr>"); sb.Append("<tr>"); foreach (var item in TotalItem.SummaryData.Keys) { sb.Append("<th>超過8秒筆數</th>"); sb.Append("<th>超過8秒502筆數</th>"); sb.Append("<th>總筆數</th>"); sb.Append("<th>502筆數</th>"); } sb.Append("<th>超過8秒筆數</th>"); sb.Append("<th>總筆數</th>"); sb.Append("</tr>"); foreach (var i in SummaryList) { sb.AppendFormat("<tr><td><span style='font-weight:bolder;'>{0}</span></td>", i.ReportServerName); foreach (var k in i.SummaryData.Keys) { sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData[k].OverTotal); sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData[k].OverError502); sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData[k].Total); sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData[k].Error502); } sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData.Sum(m => m.Value.OverTotal)); sb.AppendFormat("<td valign='top'><span style='font-weight:bolder;'>{0}</span></td>", i.SummaryData.Sum(m => m.Value.Total)); sb.AppendLine("</tr>"); } sb.AppendLine("</table>"); return(sb); }
public void ParseFile(string fileName) { //https://dotnet-snippets.de/snippet/apache-log-file-parsen-regex/5969 string logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+|-) \"([^\"]+)\" \"([^\"]+)\""; DateTime CheckStartTime = new DateTime(2015, 5, 21, 5, 05, 20); DateTime CheckEndTime = new DateTime(2015, 5, 21, 5, 06, 20); List <LogItem> list = new List <LogItem>(); using (StreamReader sr = File.OpenText(fileName)) { string s = String.Empty; while ((s = sr.ReadLine()) != null) { //we're just testing read speeds Match regexMatch = Regex.Match(s, logEntryPattern); DateTime time; if (!DateTime.TryParseExact(regexMatch.Groups[4].Value, "dd/MMM/yyyy:HH:mm:ss zzz", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out time)) { continue; } if (CheckStartTime > time) { continue; } if (CheckEndTime < time) { continue; } string referer = ""; if (!regexMatch.Groups[8].Value.Equals("-")) { referer = regexMatch.Groups[8].Value; } list.Add( new LogItem() { IPAddress = regexMatch.Groups[1].Value, DateTime = time, Request = regexMatch.Groups[5].Value, Response = regexMatch.Groups[6].Value, BytesSent = regexMatch.Groups[7].Value, Referer = referer, Browser = regexMatch.Groups[9].Value, OriginLine = s, PrivilegeLevel = SetRecPrivilege(regexMatch.Groups[5].Value) } ); } } SubtotalItem subtotal = new SubtotalItem("Test"); foreach (var i in list) { string apiName = "Others"; if (i.Request == "GET /robots.txt HTTP/1.1") { apiName = "robots"; } if (i.Request == "GET /projects/xdotool/ HTTP/1.1") { apiName = "xdotool"; } subtotal.Statistics(apiName, i.Response); } SummaryList.Add(subtotal); CreateHtmlFile(list); SendMail(); }