Beispiel #1
0
        public ActionResult CountUniqueLogs(PostedFileModel model)
        {
            try
            {
                var    p = new ApacheLogsParser();
                string s;

                int total = 0;
                List <WebVisitorModel> visitors = new List <WebVisitorModel>();
                WebVisitorModel        v        = null;

                using (StreamReader sr = new StreamReader(Request.Files[0].InputStream))
                {
                    while ((s = sr.ReadLine()) != null)
                    {
                        total++;
                        try
                        {
                            v = p.ParseLine(s);
                            visitors.Add(v);
                        }
                        catch (Exception pex)
                        {
                            Logger.Error("Error parsing line: " + s, pex);
                        }
                    }
                }

                Logger.InfoFormat("Parsed {0} visitors out of {1} lines", visitors.Count, total);

                var ipAndDate = visitors.Select(x => String.Concat(x.Ip, "_", x.VisitDate.ToString("yyyyMMdd_HHmm"))).ToArray();
                //for (int i = 0; i < 20; i++)
                //{
                //    Logger.Debug(ipAndDate[i]);
                //}
                Logger.InfoFormat("More less {0} unique visits", ipAndDate.Distinct().Count());

                return(View());
            }
            catch (Exception ex)
            {
                Logger.Error("Error parsing logs file", ex);
                throw;
            }
        }
Beispiel #2
0
        public WebVisitorModel ParseLine(string s)
        {
            // Expected: 78.57.216.1 - - [25/Oct/2015:01:02:29 -0500] "GET /js/jquery-1.5.1.min.js HTTP/1.1" 200 85275 "http://www.host.com/some/script?params" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36"
            WebVisitorModel v = new WebVisitorModel();

            int start = 0;
            int end   = 0;

            end  = s.IndexOf(' ');
            v.Ip = GetIp(s.Substring(0, end));

            start       = s.IndexOf('[');
            end         = s.IndexOf(']');
            v.VisitDate = GetVisitDate(s.Substring(start, end - start));

            start             = s.IndexOf('"', end);
            end               = s.IndexOf('"', start + 1);
            v.ResourceVisited = GetVisitedResource(s.Substring(start, end - start));

            v.HttpResponseCode = int.Parse(s.Substring(end + 2, 3));

            v.BytesDownloaded = ParseBytesDownloaded(s.Substring(end + 6));

            // In case we have referer and user-agent
            start = s.IndexOf('"', end + 1);
            if (start > 0)
            {
                // Referer
                end          = s.IndexOf('"', start + 1);
                v.RefererUrl = s.Substring(start, end - start).Trim(new char[] { '"' });

                // User-agent after referer
                start = s.IndexOf('"', end + 1);
                if (start > 0)
                {
                    end         = s.LastIndexOf('"');
                    v.UserAgent = s.Substring(start + 1, end - start - 1);
                }
            }

            return(v);
        }