Limit EventLogs by date

I grab the event logs and then display them in a datagrid, however for large logs I need to return forever, so I would like to limit the logs in the last 24 hours, but I'm not sure how to do it. I would like to limit the collection to repeating each entry, because it would still take so long. Any help would be greatly appreciated.

namespace SysTools
{
    public partial class LogViewer : Form
    {
        DataTable eventLog = new DataTable();
        DataSet dataset1 = new DataSet();
        private EventLog unhandledLogs;
        public LogViewer(EventLog logs)
        {
            unhandledLogs = logs;
            InitializeComponent();
        }

        private void LogViewer_Load(object sender, EventArgs e)
        {
            String currentLog = unhandledLogs.Log;
            DataTable dataTable1 = new DataTable();
            DataColumn column;
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Level";
            dataTable1.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Category";
            dataTable1.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.DateTime");
            column.ColumnName = "DateTime";
            dataTable1.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Message";
            dataTable1.Columns.Add(column);
            dataTable1.Rows.Clear();
            DateTime systemtime = new DateTime();
           Int32 count = unhandledLogs.Entries.Count;
            for (int currLogIndex = 0; currLogIndex <= unhandledLogs.Entries.Count; currLogIndex++)
            {
                DataRow drnew = dataTable1.NewRow();
                try
                {
                    EventLogEntry currLogEntrys = unhandledLogs.Entries[currLogIndex];
                    EventLogEntry currLogEntry = currLogEntrys;
                    string entrytype = currLogEntrys.EntryType.ToString();
                    drnew["Level"] = entrytype;
                    drnew["Category"] = currLogEntry.Source;
                    drnew["DateTime"] = currLogEntry.TimeGenerated;
                    drnew["Message"] = currLogEntry.Message;
                    dataTable1.Rows.Add(drnew);
                }
                catch { }
            }
            dataGridView1.DataSource = dataTable1;
            dataTable1.DefaultView.Sort = ("DateTime asc");
        }
    }
}
+4
source share
1 answer

EventLogQuery EventLogReader. 24 . .

. - , ( ), , , .

    public void GetEvents()
    {
        string FormattedDateTime = string.Format("{0}-{1}-{2}T{3}:{4}:{5}.000000000Z",
            DateTime.Now.Year,
            DateTime.Now.Month.ToString("D2"),
            DateTime.Now.AddDays(-1).Day.ToString("D2"),
            DateTime.Now.Hour.ToString("D2"),
            DateTime.Now.Minute.ToString("D2"),
            DateTime.Now.Second.ToString("D2"));

        string LogSource = @"Application";
        string Query = "*[System[TimeCreated[@SystemTime >= '" + FormattedDateTime + "']]]";

        var QueryResult = new EventLogQuery(LogSource, PathType.LogName, Query);
        var Reader = new System.Diagnostics.Eventing.Reader.EventLogReader(QueryResult);

        List<EventRecord> Events = new List<EventRecord>();

        bool Reading = true;

        while (Reading)
        {
            EventRecord Rec = Reader.ReadEvent();

            if (Rec == null)
                Reading = false;

            Events.Add(Rec);
            // You could add to your own collection here instead of adding to a list

        }
    }
+2

Source: https://habr.com/ru/post/1543583/


All Articles