如何在C#中反向显示事件输出

本文关键字:显示 事件 输出 | 更新日期: 2023-09-27 17:58:03

当我执行下面的代码时,它执行从2012年2月2日开始的输出,我想显示从今天到昨天,再到前天的输出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @event
{
    using System.Diagnostics;
    class MySample
    {
        public static void Main()
        {
            string eventLogName = "System";
            string sourceName = "BTHUSB";
            string machineName =".";
            EventLog eventLog;
            eventLog = new EventLog();
            eventLog.Log = eventLogName;
            eventLog.Source = sourceName;
            eventLog.MachineName = machineName;
            int i;
            i = 0;
            foreach (EventLogEntry log in eventLog.Entries)
            {
                i = i + 1;
                if(log.EntryType.ToString()=="Error")
                Console.WriteLine((i)+") Entry type: {0} , Category: {1},  Data: {2}, ID: {3}, Source: {4} 'n",log.EntryType,log.TimeWritten.ToLocalTime(),log.Data,log.EventID,log.Source);
            }
        }
    }
}

请帮我解决这个问题。。。。

如何在C#中反向显示事件输出

一个简单的反向调用就可以完成:

foreach (var log in eventLog.Entries.Cast<EventLogEntry>().Reverse()) {...}