Exemplo n.º 1
0
        public override async Task <LogResponse> FindAndSetLoggingFileAndCreateResponseObject()
        {
            var response = new LogResponse();

            response.LoggingEnabled    = true;
            response.SettingsFile      = Environment.ExpandEnvironmentVariables(@"/home/site/wwwroot/.user.ini");
            response.SettingsFileFound = File.Exists(response.SettingsFile);

            if (response.SettingsFileFound)
            {
                response.LogFile = await GetLogFile(response.SettingsFile);
            }
            else
            {
                response.LogFile = "";
            }

            if (!File.Exists(response.LogFile))
            {
                response.LogFile = @"/home/LogFiles/php-error.log";
            }
            response.LogFileFound = File.Exists(response.LogFile);

            Util.WriteLog("response.LogFile: " + response.LogFile);
            Util.WriteLog("response.LogFileFound: " + response.LogFileFound);
            //overwrite to test locally
            //response.LogFile = @"D:\Home\site\wwwroot\php_errors.log";
            //response.LogFileFound = File.Exists(response.LogFile);
            return(response);
        }
Exemplo n.º 2
0
 public static DateTime ConvertToUtcForLinux(DateTime dateTime, string tzi)
 {
     Util.WriteLog("ConvertToUtcForLinux");
     try
     {
         var timeZone = DateTimeZoneProviders.Tzdb[tzi];
         var instant  = Instant.FromDateTimeOffset(dateTime);
         var zdt      = instant.InZone(timeZone);
         var dt       = zdt.ToDateTimeUtc();
         return(dt);
     }
     catch (Exception ex)
     {
         Util.WriteLog(ex.StackTrace);
     }
     return(new DateTime());
 }
Exemplo n.º 3
0
        public long BinarySearchLogFile(Stream stream, DateTime dateToQuery, long startingOffset, long endingOffSet, TimeSpan timespan, LogParserParameters parameters)
        {
            long middle = (endingOffSet - startingOffset) / 2L;

            Util.WriteLog("BinarySearchLogFile(Stream stream, DateTime dateToQuery, long startingOffset, long endingOffSet, TimeSpan timespan)");
            Util.WriteLog("middle: " + middle);
            if (middle < 0L)
            {
                return(-1L);
            }
            stream.Seek(startingOffset + middle, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(stream))
            {
                string readLine;
                for (string str = ""; !reader.EndOfStream; str = str + readLine)
                {
                    readLine = reader.ReadLine();
                    Util.WriteLog("readLine: " + readLine);
                    DateTime dateFromLog = this.GetDateFromLog(readLine, parameters);
                    if (dateFromLog != new DateTime())
                    {
                        TimeSpan span         = dateFromLog.Subtract(dateToQuery);
                        double   totalMinutes = Math.Abs(span.TotalMinutes);
                        if ((totalMinutes <= timespan.TotalMinutes))
                        {
                            return(startingOffset + middle + str.Length);
                        }
                        if (span.TotalMinutes > timespan.TotalMinutes)
                        {
                            long offset = startingOffset + str.Length;
                            return(this.BinarySearchLogFile(stream, dateToQuery, offset, startingOffset + middle, timespan, parameters));
                        }
                        if ((span.TotalMinutes > 0.0) && (span.TotalMinutes < timespan.TotalMinutes))
                        {
                            return(startingOffset + str.Length);
                        }
                        if (span.TotalMinutes < 0.0)
                        {
                            long offset = (startingOffset + middle);
                            return(this.BinarySearchLogFile(stream, dateToQuery, offset, endingOffSet, timespan, parameters));
                        }
                    }
                }
            }
            return(0L);
        }
Exemplo n.º 4
0
        public override DateTime GetDateFromLog(string line, LogParserParameters parameters)
        {
            Util.WriteLog("GetDateFromLog(string line, LogParserParameters parameters)");
            var dateBracket = line.IndexOf("]");

            if (!line.StartsWith("[") || dateBracket == -1)
            {
                Util.WriteLog("returning: " + (new DateTime()));
                return(new DateTime());
            }


            string dateString = string.Join(" ", line.Substring(1, dateBracket - 1).Split().Take(2));

            Util.WriteLog("dateString: " + dateString);
            string timeZone = line.Substring(1, dateBracket - 1).Split()[2];

            Util.WriteLog("timeZone: " + timeZone);

            DateTime apiDate = DateTime.Parse(dateString + ((timeZone == "UTC") ? "Z" : ""));

            if (parameters.WorkerType == WorkerType.Linux)
            {
                return(Util.ConvertToUtcForLinux(apiDate, timeZone));
            }
            else
            {
                if (TimeZoneInfo == null)
                {
                    TimeZoneInfo = Util.OlsonTimeZoneToTimeZoneInfo(timeZone);
                }
                Util.WriteLog("TimeZone " + TimeZoneInfo.DisplayName);
                if (timeZone == "UTC")
                {
                    return(TimeZoneInfo.ConvertTime(apiDate, TimeZoneInfo));
                }

                return(TimeZoneInfo.ConvertTimeToUtc(apiDate, TimeZoneInfo));
            }
        }
Exemplo n.º 5
0
        public static TimeZoneInfo OlsonTimeZoneToTimeZoneInfo(string olsonTimeZoneId)
        {
            Util.WriteLog("OlsonTimeZoneToTimeZoneInfo(string olsonTimeZoneId)");
            Util.WriteLog(olsonTimeZoneId);

            var tzi = DateTimeZoneProviders.Tzdb[olsonTimeZoneId];

            var olsonWindowsTimes = new Dictionary <string, string>()
            {
                { "Africa/Bangui", "W. Central Africa Standard Time" },
                { "Africa/Cairo", "Egypt Standard Time" },
                { "Africa/Casablanca", "Morocco Standard Time" },
                { "Africa/Harare", "South Africa Standard Time" },
                { "Africa/Johannesburg", "South Africa Standard Time" },
                { "Africa/Lagos", "W. Central Africa Standard Time" },
                { "Africa/Monrovia", "Greenwich Standard Time" },
                { "Africa/Nairobi", "E. Africa Standard Time" },
                { "Africa/Windhoek", "Namibia Standard Time" },
                { "America/Anchorage", "Alaskan Standard Time" },
                { "America/Argentina/San_Juan", "Argentina Standard Time" },
                { "America/Asuncion", "Paraguay Standard Time" },
                { "America/Bahia", "Bahia Standard Time" },
                { "America/Bogota", "SA Pacific Standard Time" },
                { "America/Buenos_Aires", "Argentina Standard Time" },
                { "America/Caracas", "Venezuela Standard Time" },
                { "America/Cayenne", "SA Eastern Standard Time" },
                { "America/Chicago", "Central Standard Time" },
                { "America/Chihuahua", "Mountain Standard Time (Mexico)" },
                { "America/Cuiaba", "Central Brazilian Standard Time" },
                { "America/Denver", "Mountain Standard Time" },
                { "America/Fortaleza", "SA Eastern Standard Time" },
                { "America/Godthab", "Greenland Standard Time" },
                { "America/Guatemala", "Central America Standard Time" },
                { "America/Halifax", "Atlantic Standard Time" },
                { "America/Indianapolis", "US Eastern Standard Time" },
                { "America/Indiana/Indianapolis", "US Eastern Standard Time" },
                { "America/La_Paz", "SA Western Standard Time" },
                { "America/Los_Angeles", "Pacific Standard Time" },
                { "America/Mexico_City", "Mexico Standard Time" },
                { "America/Montevideo", "Montevideo Standard Time" },
                { "America/New_York", "Eastern Standard Time" },
                { "America/Noronha", "UTC-02" },
                { "America/Phoenix", "US Mountain Standard Time" },
                { "America/Regina", "Canada Central Standard Time" },
                { "America/Santa_Isabel", "Pacific Standard Time (Mexico)" },
                { "America/Santiago", "Pacific SA Standard Time" },
                { "America/Sao_Paulo", "E. South America Standard Time" },
                { "America/St_Johns", "Newfoundland Standard Time" },
                { "America/Tijuana", "Pacific Standard Time" },
                { "Antarctica/McMurdo", "New Zealand Standard Time" },
                { "Atlantic/South_Georgia", "UTC-02" },
                { "Asia/Almaty", "Central Asia Standard Time" },
                { "Asia/Amman", "Jordan Standard Time" },
                { "Asia/Baghdad", "Arabic Standard Time" },
                { "Asia/Baku", "Azerbaijan Standard Time" },
                { "Asia/Bangkok", "SE Asia Standard Time" },
                { "Asia/Beirut", "Middle East Standard Time" },
                { "Asia/Calcutta", "India Standard Time" },
                { "Asia/Colombo", "Sri Lanka Standard Time" },
                { "Asia/Damascus", "Syria Standard Time" },
                { "Asia/Dhaka", "Bangladesh Standard Time" },
                { "Asia/Dubai", "Arabian Standard Time" },
                { "Asia/Irkutsk", "North Asia East Standard Time" },
                { "Asia/Jerusalem", "Israel Standard Time" },
                { "Asia/Kabul", "Afghanistan Standard Time" },
                { "Asia/Kamchatka", "Kamchatka Standard Time" },
                { "Asia/Karachi", "Pakistan Standard Time" },
                { "Asia/Katmandu", "Nepal Standard Time" },
                { "Asia/Kolkata", "India Standard Time" },
                { "Asia/Krasnoyarsk", "North Asia Standard Time" },
                { "Asia/Kuala_Lumpur", "Singapore Standard Time" },
                { "Asia/Kuwait", "Arab Standard Time" },
                { "Asia/Magadan", "Magadan Standard Time" },
                { "Asia/Muscat", "Arabian Standard Time" },
                { "Asia/Novosibirsk", "N. Central Asia Standard Time" },
                { "Asia/Oral", "West Asia Standard Time" },
                { "Asia/Rangoon", "Myanmar Standard Time" },
                { "Asia/Riyadh", "Arab Standard Time" },
                { "Asia/Seoul", "Korea Standard Time" },
                { "Asia/Shanghai", "China Standard Time" },
                { "Asia/Singapore", "Singapore Standard Time" },
                { "Asia/Taipei", "Taipei Standard Time" },
                { "Asia/Tashkent", "West Asia Standard Time" },
                { "Asia/Tbilisi", "Georgian Standard Time" },
                { "Asia/Tehran", "Iran Standard Time" },
                { "Asia/Tokyo", "Tokyo Standard Time" },
                { "Asia/Ulaanbaatar", "Ulaanbaatar Standard Time" },
                { "Asia/Vladivostok", "Vladivostok Standard Time" },
                { "Asia/Yakutsk", "Yakutsk Standard Time" },
                { "Asia/Yekaterinburg", "Ekaterinburg Standard Time" },
                { "Asia/Yerevan", "Armenian Standard Time" },
                { "Atlantic/Azores", "Azores Standard Time" },
                { "Atlantic/Cape_Verde", "Cape Verde Standard Time" },
                { "Atlantic/Reykjavik", "Greenwich Standard Time" },
                { "Australia/Adelaide", "Cen. Australia Standard Time" },
                { "Australia/Brisbane", "E. Australia Standard Time" },
                { "Australia/Darwin", "AUS Central Standard Time" },
                { "Australia/Hobart", "Tasmania Standard Time" },
                { "Australia/Perth", "W. Australia Standard Time" },
                { "Australia/Sydney", "AUS Eastern Standard Time" },
                { "Etc/GMT", "UTC" },
                { "Etc/GMT+11", "UTC-11" },
                { "Etc/GMT+12", "Dateline Standard Time" },
                { "Etc/GMT+2", "UTC-02" },
                { "Etc/GMT-12", "UTC+12" },
                { "Europe/Amsterdam", "W. Europe Standard Time" },
                { "Europe/Athens", "GTB Standard Time" },
                { "Europe/Belgrade", "Central Europe Standard Time" },
                { "Europe/Berlin", "W. Europe Standard Time" },
                { "Europe/Brussels", "Romance Standard Time" },
                { "Europe/Budapest", "Central Europe Standard Time" },
                { "Europe/Dublin", "GMT Standard Time" },
                { "Europe/Helsinki", "FLE Standard Time" },
                { "Europe/Istanbul", "GTB Standard Time" },
                { "Europe/Kiev", "FLE Standard Time" },
                { "Europe/London", "GMT Standard Time" },
                { "Europe/Minsk", "E. Europe Standard Time" },
                { "Europe/Moscow", "Russian Standard Time" },
                { "Europe/Paris", "Romance Standard Time" },
                { "Europe/Sarajevo", "Central European Standard Time" },
                { "Europe/Warsaw", "Central European Standard Time" },
                { "Indian/Mauritius", "Mauritius Standard Time" },
                { "Pacific/Apia", "Samoa Standard Time" },
                { "Pacific/Auckland", "New Zealand Standard Time" },
                { "Pacific/Fiji", "Fiji Standard Time" },
                { "Pacific/Guadalcanal", "Central Pacific Standard Time" },
                { "Pacific/Guam", "West Pacific Standard Time" },
                { "Pacific/Honolulu", "Hawaiian Standard Time" },
                { "Pacific/Pago_Pago", "UTC-11" },
                { "Pacific/Port_Moresby", "West Pacific Standard Time" },
                { "Pacific/Tongatapu", "Tonga Standard Time" },
                { "UTC", "UTC" },
            };

            var windowsTimeZoneId = default(string);
            var windowsTimeZone   = default(TimeZoneInfo);

            if (olsonWindowsTimes.TryGetValue(olsonTimeZoneId, out windowsTimeZoneId))
            {
                Util.WriteLog("windowsTimeZoneId " + windowsTimeZoneId);
                try
                {
                    windowsTimeZone = TimeZoneInfo.FindSystemTimeZoneById(windowsTimeZoneId);
                }
                catch (TimeZoneNotFoundException tz)
                {
                    Util.WriteLog(tz.Message);
                    Util.WriteLog(tz.StackTrace);
                    return(TimeZoneInfo.Local);
                }
                catch (InvalidTimeZoneException it)
                {
                    Util.WriteLog(it.Message);
                    Util.WriteLog(it.StackTrace);
                    return(TimeZoneInfo.Local);
                }
                catch (Exception ex)
                {
                    Util.WriteLog(ex.Message);
                    Util.WriteLog(ex.StackTrace);
                    return(TimeZoneInfo.Local);
                }
            }
            return(windowsTimeZone);
        }
Exemplo n.º 6
0
        public async Task <LogResponse> GetHistogramAsync(LogParserParameters parameters)
        {
            Util.WriteLog(DateTime.Now + ": " + "GetHistogramAsync(LogParserParameters parameters)");
            Stopwatch sw = new Stopwatch();

            sw.Start();

            var response = await FindAndSetLoggingFileAndCreateResponseObject();

            response.Parameters = parameters;

            if (!response.LogFileFound)
            {
                sw.Stop();
                response.ParseTime = sw.ElapsedMilliseconds;
                return(response);
            }

            FileInfo fileInfo = new FileInfo(response.LogFile);
            long     filesize = fileInfo.Length;

            Util.WriteLog("filesize: " + filesize);

            long offSet = 0;

            using (Stream stream = File.Open(response.LogFile, FileMode.Open))
            {
                Util.WriteLog("BinarySearch");
                offSet = BinarySearchLogFile(stream, parameters.EndTime, 0, filesize, parameters.EndTime.Subtract(parameters.StartTime), parameters);
            }

            Util.WriteLog("offSet: " + offSet);

            if (offSet == -1)
            {
                sw.Stop();
                response.ParseTime = sw.ElapsedMilliseconds;
                return(response);
            }

            var logMetricsList = new Dictionary <string, LogMetrics>();

            offSet = ReadFileInReverseOrder(response.LogFile, offSet, parameters, response);

            ReadFileInOrder(offSet, parameters, logMetricsList, response);

            foreach (var category in logMetricsList.Keys)
            {
                var logCatgeorMetrics = new LogMetrics();

                logCatgeorMetrics.AddRange(from dt in logMetricsList[category]
                                           group dt by dt.Timestamp.Ticks / parameters.TimeGrain.Ticks
                                           into g
                                           select
                                           new LogMetric(Util.GetDateTimeInUtcFormat(new DateTime(g.Key * parameters.TimeGrain.Ticks)), g.ToList().Count));

                logCatgeorMetrics.Sort((x, y) => x.Timestamp.CompareTo(y.Timestamp));

                response.LogMetrics.Add(category, logCatgeorMetrics);
            }

            sw.Stop();

            response.ExceptionCount = response.ExceptionCount.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            response.ParseTime = sw.ElapsedMilliseconds;
            return(response);
        }