public IColumnizedLogLine SplitLine(ILogLineColumnizerCallback callback, ILogLine line) { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 // 03.01.2008 14:48:00.066 <rest of line> ColumnizedLogLine clogLine = new ColumnizedLogLine(); clogLine.LogLine = line; Column[] columns = new Column[3] { new Column { FullValue = "", Parent = clogLine }, new Column { FullValue = "", Parent = clogLine }, new Column { FullValue = "", Parent = clogLine }, }; clogLine.ColumnValues = columns.Select(a => a as IColumn).ToArray(); string temp = line.FullLine; if (temp.Length < 21) { columns[2].FullValue = temp; return(clogLine); } FormatInfo formatInfo = DetermineDateTimeFormatInfo(line); if (formatInfo == null) { columns[2].FullValue = temp; return(clogLine); } int endPos = formatInfo.DateTimeFormat.Length; int timeLen = formatInfo.TimeFormat.Length; int dateLen = formatInfo.DateFormat.Length; try { if (this.timeOffset != 0) { DateTime dateTime = DateTime.ParseExact(temp.Substring(0, endPos), formatInfo.DateTimeFormat, formatInfo.CultureInfo); dateTime = dateTime.Add(new TimeSpan(0, 0, 0, 0, this.timeOffset)); string newDate = dateTime.ToString(formatInfo.DateTimeFormat, formatInfo.CultureInfo); columns[0].FullValue = newDate.Substring(0, dateLen); // date columns[1].FullValue = newDate.Substring(dateLen + 1, timeLen); // time columns[2].FullValue = temp.Substring(endPos); // rest of line } else { columns[0].FullValue = temp.Substring(0, dateLen); // date columns[1].FullValue = temp.Substring(dateLen + 1, timeLen); // time columns[2].FullValue = temp.Substring(endPos); // rest of line } } catch (Exception) { columns[0].FullValue = "n/a"; columns[1].FullValue = "n/a"; columns[2].FullValue = temp; } return(clogLine); }
public IColumnizedLogLine SplitLine(ILogLineColumnizerCallback callback, ILogLine line) { ColumnizedLogLine cLogLine = new ColumnizedLogLine { LogLine = line }; Column[] columns = new Column[8] { new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine }, new Column { FullValue = "", Parent = cLogLine } }; cLogLine.ColumnValues = columns.Select(a => a as IColumn).ToArray(); string temp = line.FullLine; if (temp.Length > 1024) { // spam temp = temp.Substring(0, 1024); columns[3].FullValue = temp; return(cLogLine); } // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // anon-212-34-174-126.suchen.de - - [08/Mar/2008:00:41:10 +0100] "GET /wiki/index.php?title=Bild:Poster_small.jpg&printable=yes&printable=yes HTTP/1.1" 304 0 "http://www.captain-kloppi.de/wiki/index.php?title=Bild:Poster_small.jpg&printable=yes" "gonzo1[P] +http://www.suchen.de/faq.html" if (this.lineRegex.IsMatch(temp)) { Match match = this.lineRegex.Match(temp); GroupCollection groups = match.Groups; if (groups.Count == 10) { columns[0].FullValue = groups[1].Value; columns[1].FullValue = groups[3].Value; columns[3].FullValue = groups[5].Value; columns[4].FullValue = groups[6].Value; columns[5].FullValue = groups[7].Value; columns[6].FullValue = groups[8].Value; columns[7].FullValue = groups[9].Value; string dateTimeStr = groups[4].Value.Substring(1, 26); // dirty probing of date/time format (much faster than DateTime.ParseExact() if (dateTimeStr[2] == '/' && dateTimeStr[6] == '/' && dateTimeStr[11] == ':') { if (this.timeOffset != 0) { try { DateTime dateTime = DateTime.ParseExact(dateTimeStr, "dd/MMM/yyyy:HH:mm:ss zzz", new CultureInfo("en-US")); dateTime = dateTime.Add(new TimeSpan(0, 0, 0, 0, this.timeOffset)); string newDate = dateTime.ToString("dd/MMM/yyyy:HH:mm:ss zzz", new CultureInfo("en-US")); columns[2].FullValue = newDate; } catch (Exception) { columns[2].FullValue = "n/a"; } } else { columns[2].FullValue = dateTimeStr; } } else { columns[2].FullValue = dateTimeStr; } } } else { columns[3].FullValue = temp; } return(cLogLine); }
public IColumnizedLogLine SplitLine(ILogLineColumnizerCallback callback, ILogLine line) { ColumnizedLogLine clogLine = new ColumnizedLogLine(); clogLine.LogLine = line; Column[] columns = Column.CreateColumns(Log4jXmlColumnizer.COLUMN_COUNT, clogLine); // If the line is too short (i.e. does not follow the format for this columnizer) return the whole line content // in colum 8 (the log message column). Date and time column will be left blank. if (line.FullLine.Length < 15) { columns[8].FullValue = line.FullLine; } else { try { DateTime dateTime = GetTimestamp(callback, line); if (dateTime == DateTime.MinValue) { columns[8].FullValue = line.FullLine; } string newDate = dateTime.ToString(DATETIME_FORMAT); columns[0].FullValue = newDate; } catch (Exception) { columns[0].FullValue = "n/a"; } Column timestmp = columns[0]; string[] cols; cols = line.FullLine.Split(this.trimChars, Log4jXmlColumnizer.COLUMN_COUNT, StringSplitOptions.None); if (cols.Length != Log4jXmlColumnizer.COLUMN_COUNT) { columns[0].FullValue = ""; columns[1].FullValue = ""; columns[2].FullValue = ""; columns[3].FullValue = ""; columns[4].FullValue = ""; columns[5].FullValue = ""; columns[6].FullValue = ""; columns[7].FullValue = ""; columns[8].FullValue = line.FullLine; } else { columns[0] = timestmp; for (int i = 1; i < cols.Length; i++) { columns[i].FullValue = cols[i]; } } } Column[] filteredColumns = MapColumns(columns); clogLine.ColumnValues = filteredColumns.Select(a => a as IColumn).ToArray(); return(clogLine); }
void SquareSplit(ref Column[] columns, string line, int dateLen, int timeLen, int dateTimeEndPos, ColumnizedLogLine clogLine) { List <Column> columnList = new List <Column>(); int restColumn = _columnCount; if (_isTimeExists) { columnList.Add(new Column { FullValue = line.Substring(0, dateLen), Parent = clogLine }); columnList.Add(new Column { FullValue = line.Substring(dateLen + 1, timeLen), Parent = clogLine }); restColumn -= 2; } int nextPos = dateTimeEndPos; string rest = line; for (int i = 0; i < restColumn; i++) { rest = rest.Substring(nextPos); //var fullValue = rest.Substring(0, rest.IndexOf(']')).TrimStart(new char[] {' '}).TrimEnd(new char[] { ' ' }); var trimmed = rest.TrimStart(new char[] { ' ' }); if (string.IsNullOrEmpty(trimmed) || trimmed[0] != '[' || rest.IndexOf(']') < 0 || i == restColumn - 1) { columnList.Add(new Column { FullValue = rest, Parent = clogLine }); break; } nextPos = rest.IndexOf(']') + 1; var fullValue = rest.Substring(0, nextPos); columnList.Add(new Column { FullValue = fullValue, Parent = clogLine }); } while (columnList.Count < _columnCount) { columnList.Insert(columnList.Count - 1, new Column { FullValue = "", Parent = clogLine }); } columns = columnList.ToArray(); }