Beispiel #1
0
        public LogEntry ParseLine(string line, LogSource logSource)
        {
            int currentIndex = 0;
            var logDateTime  = ParseLogTime(line, currentIndex, ref currentIndex);

            if (logDateTime == null)
            {
                return(null);
            }
            var logLevel = ParseLogLevel(line, currentIndex, ref currentIndex);

            if (logLevel == null)
            {
                return(null);
            }
            var threadInfo = TryParseThreadInfo(line, currentIndex, ref currentIndex);
            var tags       = ParseTags(line, currentIndex, ref currentIndex);


            var logEntry = new LogEntry(logSource, logDateTime.Value, logLevel.Value, tags, threadInfo);


            var firstNonSpaceIndex = line.FirstNotOf(new[] { ' ' }, currentIndex);


            var contentAfterMetadate = firstNonSpaceIndex == -1? string.Empty : line.Substring(firstNonSpaceIndex);
            var metadataContent      = line.Substring(0, currentIndex);

            logEntry.AddContent(metadataContent, contentAfterMetadate);

            return(logEntry);
        }
Beispiel #2
0
        private LogEntry ParseLineWithMissingLevelTag(string line, LogSource logFile)
        {
            string dateEndDelimiter = " ";

            if (line.Length < 15)
            {
                return(null);
            }

            int firstDelimiter = line.IndexOf(dateEndDelimiter, 15, StringComparison.InvariantCulture);

            if (firstDelimiter == -1)
            {
                return(null);
            }

            string date = line.Substring(0, firstDelimiter);

            if (!DateTime.TryParse(date, out var entryTime))
            {
                return(null);
            }

            string content = line.Substring(firstDelimiter + dateEndDelimiter.Length);

            var tags = ParseTags(content, out var lastTagIndex);

            LogEntry logFileEntry = new LogEntry(logFile, entryTime, LogLevel.Unknown, tags);

            if (lastTagIndex != -1)
            {
                var contentBeforeTags = line.Substring(0, firstDelimiter + dateEndDelimiter.Length + lastTagIndex);

                string contentWithoutTag = content.Substring(lastTagIndex);

                logFileEntry.AddContent(contentBeforeTags, contentWithoutTag);
            }
            else
            {
                logFileEntry.AddContent(string.Empty, string.Empty);
            }


            return(logFileEntry);
        }
Beispiel #3
0
        private LogEntry ParseCorrectLine(string line, LogSource logFile)
        {
            var steveLine = _newFormatLineParser.ParseLine(line, logFile);

            if (steveLine != null)
            {
                return(steveLine);
            }

            int firstDelimiter = line.IndexOf(Delimiter, StringComparison.InvariantCulture);

            if (firstDelimiter == -1)
            {
                return(null);
            }
            string dateAndType = line.Substring(0, firstDelimiter);

            int indexOfType = dateAndType.IndexOf('[');

            if (indexOfType == -1)
            {
                return(null);
            }
            int endIndexOfType = dateAndType.IndexOf(']');

            if (endIndexOfType == -1)
            {
                return(null);
            }

            string date = dateAndType.Substring(0, indexOfType);

            DateTime entryTime;

            if (!DateTime.TryParse(date, out entryTime))
            {
                return(null);
            }

            string typeString = dateAndType.Substring(indexOfType + 1, endIndexOfType - indexOfType - 1);


            if (!Enum.TryParse <LogLevel>(typeString, true, out var logLevel))
            {
                return(null);
            }

            var indexToSplitContentAndTags = firstDelimiter + Delimiter.Length;

            string content = line.Substring(indexToSplitContentAndTags);

            var tags = ParseTags(content, out var lastTagIndex);


            LogEntry logFileEntry = new LogEntry(logFile, entryTime, logLevel, tags);

            if (lastTagIndex != -1)
            {
                var    contentBeforeTags = line.Substring(0, indexToSplitContentAndTags + lastTagIndex);
                string contentWithoutTag = content.Substring(lastTagIndex);
                logFileEntry.AddContent(contentBeforeTags, contentWithoutTag);
            }
            else
            {
                logFileEntry.AddContent(string.Empty, string.Empty);
            }


            return(logFileEntry);
        }