public DailyStatistics[] GetDailyStatistics(DateTime since, params string[] keyValues) { ArrayList days = new ArrayList(); // read statistics DateTime now = DateTime.Now; DateTime date = since; if (date == DateTime.MinValue) { date = GetLogsBeginDate(); } // iterate from since to now while (date < now) { // get monthly statistics MonthlyStatistics stats = GetMonthlyStatistics(date.Year, date.Month, keyValues); foreach (int day in stats.Days.Keys) { StatsLine line = stats[day]; DailyStatistics dailyStats = new DailyStatistics(); dailyStats.Year = date.Year; dailyStats.Month = date.Month; dailyStats.Day = day; dailyStats.BytesSent = line.BytesSent; dailyStats.BytesReceived = line.BytesReceived; days.Add(dailyStats); } // advance month date = date.AddMonths(1); } return((DailyStatistics[])days.ToArray(typeof(DailyStatistics))); }
public void ParseLogs <T>() where T : LogReader { // ensure calculation logic has been initialized // with the default calculating routine if (CalculateStatisticsLine == null) { CalculateStatisticsLine += new CalculateStatsLineEventHandler(Default_CalculateStatisticLine); } // string statsDir = GetStatsFilePath(); string statsName = null; // get site state LogState logState = new LogState(statsDir + logName + ".state"); LogReader reader = (LogReader)Activator.CreateInstance(typeof(T)); reader.Open(logsPath, logState.LastAccessed, logState.Line); Hashtable monthlyLogs = new Hashtable(); while (reader.Read()) { try { // skip error and system lines if (reader.ErrorLine || reader.SystemLine) { continue; } // skip block with log data if fields aren't available if (!reader.CheckFieldsAvailable(_fields)) { continue; } // string[] dateParts = reader["date"].Split('-'); // yyyy-mm-dd int day = Int32.Parse(dateParts[2]); string[] keyValues = new string[keyFieldsLength]; // for (int i = 0; i < keyFieldsLength; i++) { keyValues[i] = reader[keyFields[i]]; } // if (ProcessKeyFields != null) { ProcessKeyFields(keyFields, keyValues, reader.LineFields, reader.LineValues); } // build stats file name statsName = GetMothlyStatsFileName(dateParts[0], dateParts[1], keyValues); // MonthlyStatistics monthlyStats = (MonthlyStatistics)monthlyLogs[statsName]; if (monthlyStats == null) { // add new statistics try { monthlyStats = new MonthlyStatistics(Path.Combine(statsDir, statsName), true); monthlyLogs[statsName] = monthlyStats; } catch (Exception ex) { // Handle an exception Log.WriteError(String.Format("LogParser: Failed to instantiate monthly stats file '{0}' at '{0}' path", statsName, statsDir), ex); // SKIP OVER THE NEXT ITERATION continue; } } // get current day from statistic StatsLine dayStats = monthlyStats[day]; if (dayStats == null) { dayStats = new StatsLine(); monthlyStats[day] = dayStats; } // perform statistics line calculation // this workaround has been added due to avoid // IIS 6 vs. IIS 7 log files calculation logic discrepancies CalculateStatisticsLine(dayStats, reader.LineFields, reader.LineValues); } catch (Exception ex) { Log.WriteError(String.Format("Failed to process line {0}, statistics directory path {1}, statistics file name {2}", reader.LogLine, statsDir, reader.LogName), ex); } } // save all accumulated statistics foreach (MonthlyStatistics monthlyStats in monthlyLogs.Values) { monthlyStats.Save(statsDir); } // save site state logState.LastAccessed = reader.LogDate; logState.Line = reader.LogLine; logState.Save(); }