public static ConcurrentDictionary <string, SortedList <decimal, LogItem> > LoadFromDir(ProcessingOptions options)
        {
            IParcingFileConfig config = options.Config;
            var result = new ConcurrentDictionary <string, SortedList <decimal, LogItem> >();
            var pOpt   = new ParallelOptions {
                CancellationToken = options.CancellationToken
            };
            var dirList = Directory.EnumerateDirectories(options.Directory, "*.*", options.SearchOption).ToList();
            var lists   = new ConcurrentDictionary <string, SortedList <decimal, LogItem> >();
            ParallelLoopResult loopResult;

            try {
                loopResult = Parallel.ForEach(dirList, pOpt, (dir) => {
                    var file = Path.Combine(dir, options.LogFileName);
                    if (File.Exists(file))
                    {
                        var list    = options.Parcer.ParceFile(file, config);
                        lists[file] = list;
                    }
                });
            } catch (OperationCanceledException) {
                return(result);
            }
            if (!loopResult.IsCompleted)
            {
                return(result);
            }
            return(lists);
        }
 private void Clear()
 {
     _mainVM.ParcingEngine = null;
     _mainVM.LogItems.Clear();
     RowSeparator    = string.Empty;
     ColumnSeparator = string.Empty;
     _config         = null;
 }
Beispiel #3
0
        public SortedList <decimal, LogItem> ParceFile(string fileName, IParcingFileConfig config)
        {
            var result = Utilities.GetDataContainer();

            result.Add(DateTime.Now.Millisecond, new LogItem {
                Date = DateTime.Now, ExecutionTime = 42, Level = "Info", Logger = "Dummy", Message = "Hello log parcer"
            });
            return(result);
        }
 private void InitConfig()
 {
     if (_mainVM.ParcingEngine == null)
     {
         return;
     }
     _config         = _mainVM.ParcingEngine.GetFileConfig();
     RowSeparator    = _config.RowSeparator;
     ColumnSeparator = (_config.ColumnSeparator != null && _config.ColumnSeparator.Length > 0)? _config.ColumnSeparator.First().ToString() : "";
 }
 private static void ProcessLogFile(ProcessingOptions options, IParcingFileConfig config, string file
                                    , Func <KeyValuePair <decimal, LogItem>, int, int> rowAction)
 {
     if (File.Exists(file))
     {
         var list      = options.Parcer.ParceFile(file, config);
         var listItems = (options.MinExecutionTime.HasValue)
             ? list.Where(item => item.Key > (decimal)options.MinExecutionTime)
             : list;
         listItems.Aggregate(0, (current, row) => rowAction(row, current));
     }
 }
        private static void ProcessLogDir(string dir, ProcessingOptions options, IParcingFileConfig config, ref ConcurrentBag <IXLWorksheet> sheets)
        {
            var sheet = (new XLWorkbook()).AddWorksheet((new DirectoryInfo(dir)).Name);
            var file  = Path.Combine(dir, options.LogFileName);

            ProcessLogFile(options, config, file, (row, i) => {
                sheet.Cell(++i, 1).Value = row.Value.Date;
                sheet.Cell(i, 2).Value   = row.Value.ExecutionTime;
                sheet.Cell(i, 3).Value   = row.Value.Level;
                if (row.Value.Message.Length < 32766)
                {
                    sheet.Cell(i, 4).Value = row.Value.Message;
                }
                sheet.Row(i).Style.Alignment.SetWrapText(false);
                return(i);
            });
            sheet.Cell(1, 1).WorksheetColumn().AdjustToContents();
            sheet.Cell(1, 2).WorksheetColumn().AdjustToContents();
            sheet.Cell(1, 3).WorksheetColumn().AdjustToContents();
            sheet.Cell(1, 4).WorksheetColumn().AdjustToContents();
            sheets.Add(sheet);
        }
        public static void DoConvertOperation(ProcessingOptions options)
        {
            IParcingFileConfig config = options.Config;
            var pOpt = new ParallelOptions {
                CancellationToken = options.CancellationToken
            };
            var sheets = new ConcurrentBag <IXLWorksheet>();
            ParallelLoopResult result;

            try {
                result = Parallel.ForEach(
                    Directory.EnumerateDirectories(options.Directory, "*.*", options.SearchOption).ToList(),
                    pOpt,
                    (dir) => ProcessLogDir(dir, options, config, ref sheets));
            } catch (OperationCanceledException) {
                return;
            }
            if (!result.IsCompleted)
            {
                return;
            }
            CreateEntireDocument(options, sheets);
        }
        public SortedList <decimal, LogItem> ParceFile(string fileName, IParcingFileConfig config)
        {
            var result       = Utilities.GetDataContainer();
            var splitOptions = StringSplitOptions.RemoveEmptyEntries;

            using (TextReader reader = new StreamReader(fileName, config.Encoding)) {
                var      sql       = new StringBuilder();
                var      firstLine = string.Empty;
                string   tmpLine   = null;
                DateTime tmpDate   = default(DateTime);
                while (reader.Peek() > -1)
                {
                    if (tmpLine != null)
                    {
                        firstLine = tmpLine;
                        tmpLine   = null;
                    }
                    else
                    {
                        firstLine = reader.ReadLine();
                    }
                    if (string.IsNullOrWhiteSpace(firstLine))
                    {
                        continue;
                    }
                    var row      = new LogItem();
                    var appender = string.Empty;
                    var tmpArr   = firstLine.Split(config.ColumnSeparator, splitOptions);
                    if (tmpArr.Length > 4)
                    {
                        appender = tmpArr[4];
                    }
                    if (appender != config.RowSeparator)
                    {
                        continue;
                    }
                    var exectime = -1m;
                    if (decimal.TryParse(tmpArr[tmpArr.Length - 2], out exectime))
                    {
                        row.ExecutionTime = exectime / 1000m;
                    }
                    if (tmpDate != default(DateTime))
                    {
                        row.Date = tmpDate;
                        tmpDate  = default(DateTime);
                    }
                    else
                    {
                        row.Date = DateTime.Parse(GetDateString(tmpArr));
                    }
                    row.Level = tmpArr[3];
                    sql.Clear();
                    while (reader.Peek() > -1)
                    {
                        tmpLine = reader.ReadLine();
                        if (string.IsNullOrWhiteSpace(tmpLine))
                        {
                            continue;
                        }
                        if (tmpLine.Contains(config.RowSeparator))
                        {
                            break;
                        }
                        tmpArr = tmpLine.Split(config.ColumnSeparator, splitOptions);
                        if (DateTime.TryParse(GetDateString(tmpArr), out tmpDate))
                        {
                            break;
                        }
                        sql.AppendLine(tmpLine);
                    }
                    row.Message = sql.ToString();
                    row.Logger  = config.RowSeparator;
                    result.Add(row.ExecutionTime, row);
                }
            }
            return(result);
        }