Ejemplo n.º 1
0
        public void CompareCurrentWithPrevious()
        {
            var files = _directoryReader.GetFiles(_directoryReader.GetCurrentDirectory(), "log_*.txt");

            if (files.Length < 2)
            {
                throw new ArgumentOutOfRangeException($"{nameof(files.Length)}={files.Length}", "Not enough log files in the the directory to do a comparison. Expecting 2 or more files.");
            }

            var mostRecentLog = (from file in files
                                 orderby DateTime.ParseExact(_pathReader.GetFileNameWithoutExtension(file).Replace("log_", ""),
                                                             "yyyy_MM_d__HH_mm_ss", CultureInfo.InvariantCulture) descending
                                 select file).Take(1).First();


            const string errorKey = "[ERR]";

            foreach (var file in files)
            {
                if (file == mostRecentLog)
                {
                    continue;
                }

                foreach (var line in _fileReader.ReadAllLines(file))
                {
                    if (line.Contains(errorKey))
                    {
                        var indexOf     = line.IndexOf(errorKey, StringComparison.Ordinal);
                        var trimmedLine = line.Remove(0, indexOf + 1 + errorKey.Length).Trim();
                        _previousScan.Add(trimmedLine);
                    }
                }
            }

            foreach (var line in _fileReader.ReadAllLines(mostRecentLog))
            {
                if (line.Contains(errorKey))
                {
                    var indexOf     = line.IndexOf(errorKey, StringComparison.Ordinal);
                    var trimmedLine = line.Remove(0, indexOf + 1 + errorKey.Length).Trim();
                    _currentScan.Add(trimmedLine);
                }
            }

            _currentScan.ExceptWith(_previousScan);

            var totalProblems = 0;

            foreach (var line in _currentScan)
            {
                _logger.Error(line);
                totalProblems++;
            }

            if (totalProblems > 0)
            {
                _logger.Info($"Total problems found: {totalProblems}.");
            }
        }
Ejemplo n.º 2
0
        // Read file and return its path
        public string FilePath(HttpPostedFileBase file)
        {
            var path = "";

            try
            {
                // Get file name
                var fileName = _pathReader.GetFileNameWithoutExtension(file.FileName);
                if (String.IsNullOrEmpty(fileName))
                {
                    return("Error parsing file name.");
                }

                // Create new file name
                fileName = fileName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv";

                // Create path for new file name
                var mapPath = _pathReader.MapPath("~/Csvs");
                path = _pathReader.Combine(mapPath, fileName);
                if (String.IsNullOrEmpty(path))
                {
                    return("Error parsing file path.");
                }
            }
            catch (Exception e)
            {
                return(e.Message);
            }

            return(path);
        }