Exemple #1
0
    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);
    }