Exemplo n.º 1
0
        public void W3C_Parsing()
        {
            var @event = W3CEnumerable.FromFile("u_ex130609.log").First();

            Assert.AreEqual("::1", @event.c_ip, false, CultureInfo.InvariantCulture);
            Assert.IsNull(@event.cs_Cookie);
            Assert.IsNull(@event.cs_Referer);
            Assert.AreEqual(@"Mozilla/5.0+(compatible;+MSIE+10.0;+Windows+NT+6.2;+WOW64;+Trident/6.0)", @event.cs_User_Agent, false, CultureInfo.InvariantCulture);
            Assert.IsNull(@event.cs_bytes);
            Assert.IsNull(@event.cs_host);
            Assert.AreEqual("GET", @event.cs_method, false, CultureInfo.InvariantCulture);
            Assert.IsNull(@event.cs_uri_query);
            Assert.AreEqual(@"/", @event.cs_uri_stem, false, CultureInfo.InvariantCulture);
            Assert.IsNull(@event.cs_username);
            Assert.IsNull(@event.cs_version);
            Assert.AreEqual(DateTimeKind.Unspecified, @event.dateTime.Kind);
            Assert.AreEqual(new DateTime(635063969570000000L, DateTimeKind.Unspecified), @event.dateTime);
            Assert.IsNull(@event.s_computername);
            Assert.AreEqual("80", @event.s_port, false, CultureInfo.InvariantCulture);
            Assert.IsNull(@event.s_sitename);
            Assert.IsNull(@event.sc_bytes);
            Assert.AreEqual("200", @event.sc_status, false, CultureInfo.InvariantCulture);
            Assert.AreEqual("0", @event.sc_substatus, false, CultureInfo.InvariantCulture);
            Assert.AreEqual("0", @event.sc_win32_status, false, CultureInfo.InvariantCulture);
            Assert.AreEqual("156", @event.time_taken, false, CultureInfo.InvariantCulture);
        }
Exemplo n.º 2
0
        public void W3CbasicRead()
        {
            var en    = W3CEnumerable.FromFile("u_ex130609.log");
            int count = en.Count();

            Assert.AreEqual(17, count);
        }
Exemplo n.º 3
0
        public EmptyResult Report()
        {
            var logfilePath           = HttpContext.Server.MapPath("~/App_Data/access.log");
            var iisLog                = W3CEnumerable.FromFile(logfilePath);
            List <ReportModel> report = new List <ReportModel>();

            foreach (var line in iisLog.Where(x => !string.IsNullOrEmpty(x.c_ip) && !x.c_ip.StartsWith("207.114") && x.s_port == "80" && x.cs_method == "GET")
                     .GroupBy(x => x.c_ip)
                     .Select(group => new {
                Counter = group.Count(),
                IP = group.Key
            })
                     .OrderByDescending(x => x.Counter)
                     .ThenByDescending(x => Version.Parse(x.IP.ToString())))
            {
                ReportModel reportItem = new ReportModel();
                reportItem.Count = line.Counter;
                reportItem.IP    = IPAddress.Parse(line.IP);
                report.Add(reportItem);
            }

            CSVExporter.WriteToCSV(report);

            return(new EmptyResult());
        }
Exemplo n.º 4
0
    public static IEnumerable <W3CEvent> GetLogs()
    {
        var logPath = ConfigurationManager.AppSettings["LogPath"];
        var files   = new DirectoryInfo(logPath).GetFiles();
        var latest  = files.OrderByDescending(f => f.CreationTime).FirstOrDefault();
        var copy    = latest.FullName + "copy";

        File.Copy(latest.FullName, copy);//because IIS has lock on these files
        var iisLog = W3CEnumerable.FromFile(copy);
        var logs   = iisLog.ToList();

        File.Delete(copy);
        return(logs.OrderByDescending(o => o.dateTime));
    }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var txFile = W3CEnumerable.FromFile("access.log"); //assumes file is in the same directory as the exe

            var linqOutput = (from txresult in txFile where txresult.s_port == "80" && !txresult.s_ip.StartsWith("207.114") && txresult.cs_method == "GET" group txresult.c_ip by txresult.c_ip into h select new { Count = h.Count(), IP = h.Key });

            using (TextWriter writer = new StreamWriter(@"C:\Users\John\output.csv", false, System.Text.Encoding.UTF8)) //replace output path as desired.
                using (var csvWriter = new CsvWriter(writer))
                {
                    csvWriter.Configuration.RegisterClassMap <LogClassMap>();
                    csvWriter.WriteRecords(linqOutput);
                    writer.Flush();
                    writer.Close();
                }
        }
        public void Execute(int logFileId, string filePath)
        {
            LogFileModel logFile = _logFileRepo.GetById(logFileId);

            try
            {
                // load the contents of the file
                List <W3CEvent> logEvents;
                try
                {
                    logEvents = W3CEnumerable.FromFile(filePath).ToList();
                }
                catch (Exception)
                {
                    throw new FileFormatException("File is not a valid IIS log file");
                }


                // save the requests
                _createRequestBatchCommand.Execute(logFileId, logEvents);

                // update the record count of the log file
                const string sql = "UPDATE LogFiles SET RecordCount = @RecordCount WHERE Id = @Id";
                _dbContext.ExecuteNonQuery(sql, new { Id = logFileId, RecordCount = logEvents.Count });

                // delete the file
                _fileWrap.Delete(filePath);

                // register the job that marks the file as needing aggregate processing
                _jobRegistrationService.RegisterResetProcessedLogFileJob(logFile.Id);
            }
            catch (Exception ex)
            {
                // try and update the status of the log file record, the file will be marked as in an error state
                logFile.Status   = BLL.Lookup.LogFileStatus.Error;
                logFile.ErrorMsg = ex.Message;
                string sql = "UPDATE LogFiles SET Status = @Status, ErrorMsg = @ErrorMsg WHERE Id = @Id";
                _dbContext.ExecuteNonQuery(sql, logFile);
            }
        }