コード例 #1
0
        /// <summary>
        /// Tries to initiate the correct parsers for the given reports.
        /// </summary>
        /// <param name="reportFiles">The report files to parse.</param>
        /// <returns>
        /// The IParser instance.
        /// </returns>
        public ParserResult ParseFiles(IReadOnlyCollection <string> reportFiles)
        {
            if (reportFiles == null)
            {
                throw new ArgumentNullException(nameof(reportFiles));
            }

            var result = new ParserResult();

            int counter = 0;

            try
            {
                Parallel.ForEach(
                    reportFiles,
                    new ParallelOptions()
                {
                    MaxDegreeOfParallelism = this.numberOfReportsParsedInParallel
                },
                    reportFile =>
                {
                    int number = Interlocked.Increment(ref counter);
                    Logger.InfoFormat(Resources.LoadingReport, reportFile, number, reportFiles.Count);

                    try
                    {
                        string line1 = File.ReadLines(reportFile).First();

                        IEnumerable <ParserResult> parserResults = line1.Trim().StartsWith("<")
                            ? this.ParseXmlFile(XDocument.Load(reportFile))
                            : this.ParseTextFile(File.ReadAllLines(reportFile));

                        foreach (var parserResult in parserResults)
                        {
                            lock (this.mergeLock)
                            {
                                result.Merge(parserResult);
                            }
                        }
                    }
                    catch (Exception ex) when(!(ex is UnsupportedParserException))
                    {
                        Logger.ErrorFormat(" " + Resources.ErrorDuringReadingReport, reportFile, GetHumanReadableFileSize(reportFile), ex.GetExceptionMessageForDisplay());
                    }
                });
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.Flatten().InnerExceptions)
                {
                    if (e is UnsupportedParserException)
                    {
                        throw e;
                    }
                }

                throw;
            }

            return(result);
        }