/// <summary> /// Set Highlight values to specified column value /// </summary> /// <param name="col">Target column value</param> /// <param name="defList">Highlight definition list</param> private void SetHighlights(ParsedLogColumn col, HighlightDefinitionList defList) { log.InPrivate(col, defList); if (col.ColumnDefinition.IsDateTimeField) { log.OutPrivate(); return; } List <HighlightDefinition> targetDefList = defList.Find(col.ColumnDefinition); foreach (HighlightDefinition def in targetDefList) { string key = def.Keyword; string val = col.Value; if (!def.CaseSensitive) { key = key.ToLower(); val = val.ToLower(); } if (val.Contains(key)) { SetAllHightlightItems(key, val, col, def); } } log.OutPrivate(); }
/// <summary> /// Write analyzed log to internal database /// </summary> /// <param name="fileName">Log file name</param> /// <param name="logStream">Stream of log file</param> /// <param name="enc">Log file encoding</param> public void Write(string fileName, Stream logStream, Encoding enc) { log.In(logStream); // Reset line count lineCount = 0; using (SQLiteTransaction trans = connection.BeginTransaction()) { StreamReader reader = new StreamReader(new BufferedStream(logStream), enc); FileValue file = InsertFileName(fileName); ParsedLogLine prevParsedLog = null; while (true) { string line = reader.ReadLine(); if (line == null) { break; } ParsedLogLine logLine = ParseLine(file, line, regex, columnDefList); if (logLine != null) { // If log line has error then set previous datetime value for sorting if (logLine.HasError) { ParsedLogColumn col = GetDateTimeColumn(prevParsedLog); if (col != null) { logLine.ColumnList.Add(col); } } else { prevParsedLog = logLine; } lineCount++; logLine.LineNumber = lineCount; InsertLogLine(logLine); } } trans.Commit(); } log.Out(); }
/// <summary> /// Read log lines from database /// </summary> /// <param name="criteria">Search condition</param> /// <param name="colDefList">Column definition list</param> /// <param name="highlightDefList">Highlight definition list</param> /// <param name="offset">Read start position</param> /// <param name="limit">Read log lines limit</param> /// <returns>Parsed log lines</returns> public ParsedLog ReadLines(SearchCriteria criteria, ColumnDefinitionList colDefList, HighlightDefinitionList highlightDefList, int offset, int limit) { log.In(criteria, offset, limit); SQLiteCommand comm = connection.CreateCommand(); SetSelectStatement(comm, colDefList, criteria, offset, limit); ParsedLog parsedLog = new ParsedLog(); List <ParsedLogLine> lineList = new List <ParsedLogLine>(); using (SQLiteDataReader reader = comm.ExecuteReader()) { while (reader.Read()) { ParsedLogLine logLine = new ParsedLogLine(); logLine.File = new FileValue(); logLine.File.Name = GetStringValue(reader, IntermediateLogConstants.COL_NAME_LF_FILE_NAME); logLine.LineNumber = GetIntValue(reader, IntermediateLogConstants.COL_NAME_PL_LINE_NUM); logLine.HasError = GetBooleanValue(reader, IntermediateLogConstants.COL_NAME_PL_HAS_ERROR); logLine.NotParsedLog = GetStringValue(reader, IntermediateLogConstants.COL_NAME_PL_NOT_PARSED_LINE); foreach (ColumnDefinition colDef in colDefList) { ParsedLogColumn col = new ParsedLogColumn(); col.ColumnDefinition = colDef; col.Value = GetStringValue(reader, colDef.ColumnName); if (highlightDefList != null) { SetHighlights(col, highlightDefList); } logLine.ColumnList.Add(col); } lineList.Add(logLine); } } parsedLog.TotalLineCount = GetTotalLineCount(); parsedLog.TargetLineCount = GetLineCount(criteria); parsedLog.LogLineList = lineList; log.Out(lineList); return(parsedLog); }
/// <summary> /// Set HashedValue to Parsed log column. /// If not exist hashed value then insert new value to database /// </summary> /// <param name="lineElem">Raw log element value</param> /// <param name="col">Parsed log column</param> private void SetHashedValue(string lineElem, ParsedLogColumn col) { log.InPrivate(lineElem, col); using (SQLiteTransaction trans = connection.BeginTransaction()) { long id = GetHashedValueID(lineElem); if (id == -1) { id = InsertHashedValue(lineElem); } col.Value = id.ToString(); trans.Commit(); } log.OutPrivate(); }
/// <summary> /// Set hightlight item to specified value /// </summary> /// <param name="key">Highlight keyword</param> /// <param name="val">Target value</param> /// <param name="col">Log column</param> /// <param name="def">Highlight definition</param> private void SetAllHightlightItems(string key, string val, ParsedLogColumn col, HighlightDefinition def) { log.InPrivate(key, val, col); if (key == "") { return; } string tempVal = val; int removedCount = 0; while (true) { if (tempVal.Contains(key)) { int startIndex = tempVal.IndexOf(key); Highlight highlight = new Highlight(); highlight.HighlightDefinition = def; removedCount += startIndex; highlight.StartIndex = removedCount; removedCount += key.Length; highlight.EndIndex = removedCount; col.HighlightList.Add(highlight); tempVal = tempVal.Substring(startIndex + key.Length); } else { break; } } log.OutPrivate(); }
/// <summary> /// Set date time value from database string value /// </summary> /// <param name="lineElem">Raw log element value</param> /// <param name="col">Parsed log column without datetime value</param> /// <param name="colDef">Column definition</param> /// <returns>Parsed log column with datetime value</returns> private bool SetDataTimeValue(string lineElem, ParsedLogColumn col, ColumnDefinition colDef) { log.InPrivate(lineElem, col, colDef); DateTime date; if (DateTime.TryParseExact(lineElem, colDef.DateTimeFormat, System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out date)) { col.Value = date.Ticks.ToString(); log.OutPrivate(true); return(true); } else { log.OutPrivate(false); return(false); } }
/// <summary> /// Parse log line /// </summary> /// <param name="file">Log file name</param> /// <param name="line">Target log line</param> /// <param name="regex">Analyzing regex</param> /// <param name="colDefList">Column definition list</param> /// <returns>Parsed log line</returns> private ParsedLogLine ParseLine(FileValue file, string line, Regex regex, ColumnDefinitionList colDefList) { log.InPrivate(line, regex, colDefList); ParsedLogLine logLine = new ParsedLogLine(); logLine.File = file; logLine.ColumnList = new List <ParsedLogColumn>(); string[] lineElems = regex.Split(line); lineElems = RemoveFirstAndLastEmptyElems(lineElems); if (lineElems.Length != colDefList.Count) { log.Warn("Log element size is different.Skip this line. Expected=" + colDefList.Count.ToString() + " Actual=" + lineElems.Length); logLine.HasError = true; logLine.NotParsedLog = line; log.OutPrivate(logLine); return(logLine); } int index = 0; foreach (string lineElem in lineElems) { ColumnDefinition colDef = colDefList[index]; ParsedLogColumn col = new ParsedLogColumn(); col.ColumnDefinition = colDef; if (colDef.IsDateTimeField) { if (SetDataTimeValue(lineElem, col, colDef) == false) { logLine.HasError = true; log.Warn("Could not parse DateTime value. Value=" + lineElem); } } else if (colDef.Hashable) { SetHashedValue(lineElem, col); } else { col.Value = lineElem; } logLine.ColumnList.Add(col); index++; } if (logLine.HasError || line.Trim() == "") { logLine.ColumnList.Clear(); logLine.HasError = true; logLine.NotParsedLog = line; } log.OutPrivate(logLine); return(logLine); }