Example #1
0
        /// <summary>
        /// Returns the specified number of log entries of the specified category, starting with the latest one.
        /// </summary>
        /// <param name="category">The category of the log entries to be returned  (-1 for "all").</param>
        /// <param name="maxItems">The maximum number of items returned (-1 for "all").</param>
        /// <param name="logfilePath">For environments where "AppDomain.CurrentDomain.BaseDirectory" doesn't work reliably, the path to the nLog.csv file can be explicitly specified here.</param>
        /// <returns>A list of LogEntrys or null.</returns>
        public static List <ErrorLogEntry> GetLogEntries(LogEntryCategories category, int maxItems, DateTime startTime, DateTime endTime, string searchText, string myLogfilePath = null)
        {
            Exception ex;

            myLogfilePath = myLogfilePath ?? logFilePath;
            DataTable dtEntries = CsvHandler.ReadCSVFile(myLogfilePath, out ex);

            if (dtEntries == null || ex != null)
            {
                LastException = ex;
                return(null);
            }

            bool displayAllCategories = (int)category == -1 ? true : false; //determine if all entries have to be displayed

            List <ErrorLogEntry> entriesToReturn = new List <ErrorLogEntry>();

            for (int i = dtEntries.Rows.Count - 1; i >= 0; i--) //iterate the table from bottom to top (new to old)
            {
                DateTime entryTime;
                string   categoryString = category.ToString();

                if (displayAllCategories || dtEntries.Rows[i].ItemArray[2].ToString() == categoryString) //category matches the input parameter
                {
                    if (!DateTime.TryParse(dtEntries.Rows[i].ItemArray[0].ToString(), out entryTime))
                    {
                        entryTime = endTime;
                    }

                    if (entryTime >= startTime && entryTime <= endTime)
                    {
                        if (string.IsNullOrEmpty(searchText) || (dtEntries.Rows[i].ItemArray[3].ToString().Contains(searchText) || dtEntries.Rows[i].ItemArray[4].ToString().Contains(searchText)))
                        {
                            ErrorLogEntry newLogEntry = new ErrorLogEntry()
                            {
                                Time = entryTime, LogLevel = dtEntries.Rows[i].ItemArray[1].ToString(), Category = dtEntries.Rows[i].ItemArray[2].ToString(), Message = dtEntries.Rows[i].ItemArray[3].ToString(), ExceptionText = dtEntries.Rows[i].ItemArray[4].ToString(), CodeLine = dtEntries.Rows[i].ItemArray[5].ToString()
                            };
                            entriesToReturn.Add(newLogEntry);
                        }
                    }
                }

                if (maxItems > 0 && entriesToReturn.Count == maxItems)
                {
                    break;
                }
            }

            return(entriesToReturn);
        }
Example #2
0
        /// <summary>
        /// Returns the specified number of log entries, starting with the latest one.
        /// </summary>
        /// <param name="maxItems">The maximum number of items returned (-1 for "all").</param>
        /// <returns>A list of LogEntrys or null.</returns>
        public static List <UserLogEntry> GetUserLogEntries(int maxItems, DateTime startTime, DateTime endTime)
        {
            Exception ex;
            DataTable dtEntries = CsvHandler.ReadCSVFile(Environment.CurrentDirectory + @"\Logs\nUserLog.csv", out ex);

            if (dtEntries == null || ex != null)
            {
                return(null);
            }

            List <UserLogEntry> entriesToReturn = new List <UserLogEntry>();

            for (int i = dtEntries.Rows.Count - 1; i >= 0; i--) //iterate the table from bottom to top (new to old)
            {
                DateTime entryTime;

                if (!DateTime.TryParse(dtEntries.Rows[i].ItemArray[0].ToString(), out entryTime))
                {
                    entryTime = endTime;
                }

                if (entryTime >= startTime && entryTime <= endTime)
                {
                    UserLogEntry newLogEntry = new UserLogEntry()
                    {
                        Time = entryTime, Category = dtEntries.Rows[i].ItemArray[1].ToString(), Message = dtEntries.Rows[i].ItemArray[2].ToString(), ExceptionText = dtEntries.Rows[i].ItemArray[3].ToString()
                    };
                    entriesToReturn.Add(newLogEntry);
                }

                if (maxItems > 0 && entriesToReturn.Count == maxItems)
                {
                    break;
                }
            }

            return(entriesToReturn);
        }