BlueCollar.Examples.Mvc.Models.HomeIndex.Fill C# (CSharp) Method

Fill() public method

Fills the model with data.
public Fill ( ModelStateDictionary modelState, bool isPostBack ) : bool
modelState ModelStateDictionary The to add errors to.
isPostBack bool A value indicating whether the fill is taking place during a post-back.
return bool
        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;
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// GET implementation of the Index action.
        /// </summary>
        /// <returns>The action result.</returns>
        public ActionResult Index()
        {
            HomeIndex model = new HomeIndex()
            {
                LogPath = Server.MapPath("~/App_Data/BlueCollar.log")
            };

            model.Fill(ModelState, false);
            return View(model);
        }