BlueCollar.Examples.Mvc.Models.LogEntry.Parse C# (CSharp) Метод

Parse() публичный статический Метод

Parses a line into a LogEntry.
public static Parse ( string line ) : LogEntry
line string The line to parse.
Результат LogEntry
        public static LogEntry Parse(string line)
        {
            if (string.IsNullOrEmpty(line))
            {
                throw new ArgumentNullException("line", "line must contain a value.");
            }

            Match match = Regex.Match(line, @"^(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.[^\s]+)\s+-\s+([^\s]+)\s+-\s+(.*)$", RegexOptions.Singleline);

            if (match.Success)
            {
                return new LogEntry()
                {
                    Date = DateTime.Parse(match.Groups[1].Value.Trim()),
                    Level = match.Groups[2].Value.Trim(),
                    Message = match.Groups[3].Value.Trim()
                };
            }
            else
            {
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, "The line '{0}' is not in the correct format to be parsed into a LogEntry.", line));
            }
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Fills the model with data.
        /// </summary>
        /// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors to.</param>
        /// <param name="isPostBack">A value indicating whether the fill is taking place during a post-back.</param>
        /// <returns>True if the fill succeeded, false otherwise.</returns>
        public bool Fill(ModelStateDictionary modelState, bool isPostBack)
        {
            bool success = true;

            if (File.Exists(this.LogPath))
            {
                IList <string> tail      = Tail.Read(this.LogPath, 100).ToList();
                IList <string> multiLine = new List <string>();
                IList <string> result    = new List <string>();

                foreach (string line in tail)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        if (Regex.IsMatch(line, @"^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.[^\s]+"))
                        {
                            if (multiLine.Count > 0)
                            {
                                multiLine.Add(line.Trim());
                                result.Add(string.Join("\n", multiLine.Reverse().ToArray()));
                                multiLine.Clear();
                            }
                            else
                            {
                                result.Add(line.Trim());
                            }
                        }
                        else
                        {
                            multiLine.Add(line.Trim());
                        }
                    }
                }

                this.entries = result
                               .Select(l => LogEntry.Parse(l))
                               .ToList();
            }
            else
            {
                modelState.AddModelError(string.Empty, string.Format(CultureInfo.InvariantCulture, "There was no log file found at '{0}'.", this.LogPath));
                modelState.AddModelError(string.Empty, "Note that no log file will be created if running from the console application (Collar.exe), or the service application. Logs are maintained in those situations under the appropriate Console.exe directory instead.");
                success = false;
            }

            return(success);
        }
LogEntry