Exemplo n.º 1
0
        /// <summary>
        /// Parses a managed exception message and outputs a data object with a separate message and callstack entries.
        /// </summary>
        /// <param name="message">Message to parse.</param>
        /// <returns>Parsed log message.</returns>
        public static ParsedLogEntry ParseExceptionMessage(string message)
        {
            Regex headerRegex = new Regex(@"Managed exception: (.*)\n");
            var   headerMatch = headerRegex.Match(message);

            if (!headerMatch.Success)
            {
                return(null);
            }

            Regex regex   = new Regex(@"  at (.*) \[.*\] in (.*):(\d*)");
            var   matches = regex.Matches(message);

            ParsedLogEntry newEntry = new ParsedLogEntry();

            newEntry.callstack = new CallStackEntry[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                CallStackEntry callstackEntry = new CallStackEntry();
                callstackEntry.method = matches[i].Groups[1].Value;
                callstackEntry.file   = matches[i].Groups[2].Value;
                int.TryParse(matches[i].Groups[3].Value, out callstackEntry.line);

                newEntry.callstack[i] = callstackEntry;
            }

            newEntry.message = headerMatch.Groups[1].Value;

            return(newEntry);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a log message and outputs a data object with a separate message and callstack entries.
        /// </summary>
        /// <param name="message">Message to parse.</param>
        /// <returns>Parsed log message.</returns>
        public static ParsedLogEntry ParseLogMessage(string message)
        {
            // Note: If you are modifying GetStackTrace method make sure to also update this one to match the formattting
            int   firstMatchIdx = -1;
            Regex regex         = new Regex(@"\tat (.*) in (.*), line (\d*), column .*, namespace .*");
            var   matches       = regex.Matches(message);

            ParsedLogEntry newEntry = new ParsedLogEntry();

            newEntry.callstack = new CallStackEntry[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                CallStackEntry callstackEntry = new CallStackEntry();
                callstackEntry.method = matches[i].Groups[1].Value;
                callstackEntry.file   = matches[i].Groups[2].Value;
                int.TryParse(matches[i].Groups[3].Value, out callstackEntry.line);

                newEntry.callstack[i] = callstackEntry;

                if (firstMatchIdx == -1)
                {
                    firstMatchIdx = matches[i].Index;
                }
            }

            if (firstMatchIdx != -1)
            {
                newEntry.message = message.Substring(0, firstMatchIdx);
            }
            else
            {
                newEntry.message = message;
            }

            return(newEntry);
        }