示例#1
0
        public IEnumerable<LogFileLine> ReadLogFileLines()
        {
            using (var tmp = new TempFile())
            {
                StreamReader reader;

                try
                {
                    var stream = File.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    reader = new StreamReader(stream);
                }
                catch (IOException)
                {
                    File.CopyTo(tmp.File.FullName);
                    reader = new StreamReader(tmp.File.OpenRead());
                }

                using (reader)
                    foreach (var l in reader.ReadLines().Select((l, i) =>
                        new LogFileLine
                        {
                            Text = l,
                            LineNumber = i + 1,
                            LogFile = this
                        }))
                        yield return l;
            }
        }
        public void ReadLinesTest()
        {
            var source = new[] { "a", "b", "c" };

            AssertEx.Throws<ArgumentNullException>(() => TextReaderExtensions.ReadLines(null).ToArray());

            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                foreach (var s in source)
                {
                    writer.WriteLine(s);
                }
                writer.Flush();

                stream.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    var array = reader.ReadLines().ToArray();

                    array.Count().Is(source.Count());
                    Enumerable.Range(0, array.Count()).All(i => array[i] == source[i]).IsTrue();
                }
            }
        }
示例#3
0
 private void ReadAllFactoids()
 {
     using (TextReader reader = new StreamReader(Data))
     {
         _factoids = (from factoid in reader.ReadLines()
                      let split = factoid.IndexOf(';')
                      let user = factoid.Substring(0, split)
                      let fact = factoid.Substring(split)
                      group factoid by user into factoidsPerUser
                      select factoidsPerUser
                     ).ToDictionary(x => x.Key, x => x.ToList());
     }
 }
        public FileProfilingLogParser(string logFileName)
        {
            if (string.IsNullOrEmpty(logFileName))
            {
                throw new ArgumentNullException("logFileName");
            }

            if (!File.Exists(logFileName))
            {
                throw  new ArgumentException("Log file doesn't exist: " + logFileName);
            }

            using (var inStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (var sr = new StreamReader(inStream))
            {
                _logFileLines = sr.ReadLines().ToArray();
            }
        }
示例#5
0
文件: LogParser.cs 项目: omnibs/Dlp
        public static void Load(string logFilePath, string pattern, List<string> filterPatterns, int batchId, Action<List<LogEntry>> write)
        {
            Output.WriteLine("Lendo arquivo de log: " + logFilePath);
            var r = new Regex(pattern);
            var filters = filterPatterns.Select(x => new Regex(x));

            var rdr = new StreamReader(File.OpenRead(logFilePath));

            List<string> lines;
            int[] counter = { 0 };
            while ((lines = rdr.ReadLines(10000).ToList()).Any())
            {
                Output.WriteLine(lines.Count + " linhas lidas.");

                Output.BlankLine();
                Output.Write("Filtrando... ");
                lines = lines.Where(x => !filters.Any(filter => filter.IsMatch(x))).ToList();
                Output.WriteLine(lines.Count + " linhas após filtragem.");

                Output.BlankLine();
                Output.Write("Aplicando pattern... ");
                var matches = lines
                    .Select(x => r.Match(x))
                    .OfType<Match>()
                    .Where(x => x.Success).ToList();
                Output.WriteLine(matches.Count() + " resultados encontrados.");

                Output.BlankLine();
                Output.Write("Convertendo para LogEntry... ");

                var newItems = matches
                    .Where(x => x.Groups["path"].Success && !string.IsNullOrEmpty(x.Groups["path"].Value))
                    .Select
                        (x => new LogEntry(x, counter[0]++) { BatchId = batchId })
                    .ToList();
                Output.WriteLine("OK.");

                write(newItems);
            }
        }
示例#6
0
        static void BuildDatabase(StreamReader data)
        {
            var sep = new char[] { ';' };

            database = new Dictionary<int, CharInfo>();
            ranges = new List<RangeInfo>();

            foreach(string raw_line in data.ReadLines())
            {
                int n = raw_line.IndexOf('#');
                string line = n == -1 ? raw_line : raw_line.Substring(raw_line.Length - n).Trim();

                if(string.IsNullOrEmpty(line))
                    continue;

                var info = line.Split(sep, 2);
                var m = Regex.Match(info[0], @"([0-9a-fA-F]{4})\.\.([0-9a-fA-F]{4})");
                if(m.Success)
                {
                    // this is a character range
                    int first = Convert.ToInt32(m.Groups[1].Value, 16);
                    int last = Convert.ToInt32(m.Groups[2].Value, 16);

                    ranges.Add(new RangeInfo(first, last, info[1].Split(sep)));
                }
                else
                {
                    // this is a single character
                    database[Convert.ToInt32(info[0], 16)] = new CharInfo(info[1].Split(sep));
                }
            }
        }
示例#7
0
        public static void Run(string[] args)
        {
            Log.HighLight("{0} is starting...", s_consoleName);
            try
            {
                Thread.CurrentThread.CurrentCulture = Config.DefaultCulture;
                Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

                object config;
                var configFile = "{0}.ini".FormatWith(s_consoleName);
                if (File.Exists(configFile))
                {
                    Log.Info("Loading config file: {0}", configFile);
                    using (var streamReader = new StreamReader(configFile))
                    {
                        HRONDynamicParseError[] parserErrors;
                        if (!HRONSerializer.TryParseDynamic(
                            int.MaxValue,
                            streamReader.ReadLines().Select(x => x.ToSubString()),
                            out config,
                            out parserErrors
                            ))
                        {
                            throw new ExitCodeException(ExitCode.InvalidConfigFile);
                        }
                    }
                }
                else
                {
                    config = HRONObject.Empty;
                }

                Log.Info("Initial setup is done, executing main program");

                Partial_Run(args, config);

                Log.Success("{0} completed", s_consoleName);
            }
            catch (ExitCodeException exc)
            {
                Environment.ExitCode = (int) exc.ExitCode;
                Log.Exception(
                    "Terminated {0} {1}({2:000}), caught exception: {3}",
                    s_consoleName,
                    exc.ExitCode,
                    Environment.ExitCode,
                    exc
                    );
            }
            catch (Exception exc)
            {
                Environment.ExitCode = 999;
                Log.Exception(
                    "Terminated {0} Unknown({1:000}), caught exception: {2}",
                    s_consoleName,
                    Environment.ExitCode,
                    exc
                    );
            }
        }
示例#8
0
        /// <summary>
        /// Used for pre-processed data. Parses the data from the Stream according to provided TrueValue, FalseValue and CsvSeparators.
        /// </summary>
        /// <returns>A formal context resulting from parsed data.</returns>
        public FormalContext ParseContext()
        {
            using (TextReader reader = new StreamReader(FileStream))
            {
                string[] lines = reader.ReadLines().ToArray();

                string[] attributeNames = lines[0].Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
                List<Algorithm.Attribute> attributes = CreateAttributes(attributeNames.Skip(1)).ToList();
                List<Item> items = new List<Item>();
                bool[,] matrix = new bool[lines.Length - 1, attributes.Count];

                for (int i = 1; i < lines.Length; i++)
                {
                    string[] lineValues = lines[i].Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
                    items.Add(new Item() { Name = lineValues[0] });

                    for (int j = 1; j < lineValues.Length; j++)
                    {
                        matrix[i - 1, j - 1] = ParseBool(lineValues[j]);
                    }
                }

                return new FormalContext(attributes, items, matrix, true);
            }
        }
示例#9
0
        /// <summary>
        /// Used for data, which still need pre-processing. Reads the data, parses them and finds the minimum and maximum value.
        /// Default equi-distant categorization is used, to divide the attributes into smaller sub-attributes. This causes the OnThresholdsFound event to be fired.
        /// After that, a new formal context is formed, based on the provided set of items, matrix of values and set of newly created sub-attributes.
        /// </summary>
        /// <returns>A formal context based on equi-distant categorization of provided data.</returns>
        public FormalContext PreprocessData()
        {
            using (TextReader reader = new StreamReader(FileStream))
            {
                string[] lines = reader.ReadLines().ToArray();

                for (int i = 0; i < lines.Length; i++)
                {
                    lines[i] = Regex.Replace(lines[i],
                                    @",(?=[^""]*""(?:[^""]*""[^""]*"")*[^""]*$)",
                                    String.Empty);
                }

                string[] attributeNames = lines[0].Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
                List<Algorithm.Attribute> oldAttributes = CreateAttributes(attributeNames.Skip(1)).ToList();

                List<Item> items = new List<Item>();
                int[,] oldMatrix = new int[lines.Length - 1, oldAttributes.Count];

                for (int i = 1; i < lines.Length; i++)
                {
                    string[] lineValues = lines[i].Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
                    items.Add(new Item() { Name = lineValues[0] });

                    for (int j = 1; j < lineValues.Length; j++)
                    {
                        int parsedNumber;
                        if (int.TryParse(new string(lineValues[j].ToCharArray().Where(c => char.IsDigit(c)).ToArray()), out parsedNumber))
                        {
                            oldMatrix[i - 1, j - 1] = parsedNumber;

                            if (oldAttributes[j - 1].Max < parsedNumber)
                                oldAttributes[j - 1].Max = parsedNumber;

                            if (oldAttributes[j - 1].Min > parsedNumber)
                                oldAttributes[j - 1].Min = parsedNumber;
                        }
                        else throw new InvalidCastException("Non-numeric string found, where number was expected, line " + i);
                    }
                }

                List<Algorithm.Attribute> attributes;

                if (OnThresholdsFound != null)
                    OnThresholdsFound(oldAttributes);

                bool[,] matrix = CreateNewMatrix(oldAttributes, oldMatrix, out attributes);

                return new FormalContext(attributes, items, matrix, true);
            }
        }
示例#10
0
文件: Runner.cs 项目: mrange/hron
        static TestResult GetTestResult(string testResultPath)
        {
            testResultPath = testResultPath ?? "";

            var result = new TestResult
                             {
                                 Path = testResultPath                      ,
                                 Configuration = new TestRunConfiguration() ,
                             };

            Log.Info ("Processing test result in: {0}", result.Name);

            var configurationFilePath = Path.Combine(testResultPath, ParserValidatorConfig);
            var configurationFileName = Path.GetFileName (configurationFilePath);
            if (File.Exists(configurationFilePath))
            {
                Log.Info("Reading config file: {0}", configurationFileName);
                using (var configStream = new StreamReader(configurationFilePath))
                {
                    dynamic config;
                    HRONDynamicParseError[] errors;
                    var parseResult = HRONSerializer.TryParseDynamic(
                        int.MaxValue,
                        configStream.ReadLines().Select (s => s.ToSubString ()),
                        out config,
                        out errors
                        );

                    if (!parseResult)
                    {
                        Log.Error("Failed to read config file: {0}", configurationFileName);
                        return result;
                    }

                    result.Configuration.Equivalence.EmptyContentLinesAndEmptyLines     = config.Equivalence.EmptyContentLinesAndEmptyLines ;
                    result.Configuration.Equivalence.CommentsAndCommentLines            = config.Equivalence.CommentsAndCommentLines        ;
                    result.Configuration.PreProcessor   = config.PreProcessor   ;
                    result.Configuration.Object_Begin   = config.Object_Begin   ;
                    result.Configuration.Object_End     = config.Object_End     ;
                    result.Configuration.Value_Begin    = config.Value_Begin    ;
                    result.Configuration.Value_End      = config.Value_End      ;
                    result.Configuration.Comment        = config.Comment        ;
                    result.Configuration.Empty          = config.Empty          ;
                    result.Configuration.ContentLine    = config.ContentLine    ;
                    result.Configuration.EmptyLine      = config.EmptyLine      ;
                    result.Configuration.CommentLine    = config.CommentLine    ;
                }
            }

            result.ActionLogs = Directory
                .EnumerateFiles(testResultPath, "*.actionlog")
                .Select(p => new TestResultActionLog {ActionLogPath = p})
                .ToArray()
                ;

            return result;
        }