C# 如何在ASP.NET中读取应用程序日志并根据源进行过滤?

C# 如何在ASP.NET中读取应用程序日志并根据源进行过滤?,c#,asp.net,events,C#,Asp.net,Events,在我的应用程序中,我想读取本地系统的应用程序事件日志。 目前我正在使用以下代码 public partial class AppLog : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { System.Diagnostics.EventLog logInfo = new System.

在我的应用程序中,我想读取本地系统的应用程序事件日志。 目前我正在使用以下代码

public partial class AppLog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            System.Diagnostics.EventLog logInfo = new System.Diagnostics.EventLog();
            logInfo.Log = "Application";
            logInfo.MachineName = ".";  // Local machine
            string strImage = "";  // Icon for the event
            Response.Write("<p>There are  " + logInfo.Entries.Count + " entries in the System event log.</p>");


            foreach (EventLogEntry entry in logInfo.Entries.Cast<EventLogEntry>().Reverse<EventLogEntry>())            
            {
                switch (entry.EntryType)
                {
                    case EventLogEntryType.Warning:
                        strImage = "images/icon_warning.PNG";
                        break;
                    case EventLogEntryType.Error:
                        strImage = "images/icon_error.PNG";
                        break;
                    default:
                        strImage = "images/icon_info.PNG";
                        break;
                }
                Response.Write("<img src=\"" + strImage + "\">&nbsp;|&nbsp;");
                Response.Write(entry.TimeGenerated.ToString() + "&nbsp;|&nbsp;");
                Response.Write(entry.Message.ToString() + "<br>\r\n");
            }
        }
    }
}
并遍历结果列表。

查看这些选项是否有帮助

仅供参考:是你的反向()导致了减速。 Linq有助于过滤,只要以后不反转它

我最终做这件事是为了我的目的:

for(int x = logInfo.Entries.Count-1; x >= 0; x--)
        {
          EventLogEntry entry = (EventLogEntry)logInfo.Entries[x]; ...
再解释一下,对于循环的每一次迭代,都调用reverse(),这是一个缓慢而密集的过程。如果在foreach循环之前反转,您可能能够加快速度。

var eventLog=new eventLog(“应用程序”);
var eventLog = new EventLog("Application");
eventLog.Entries
        .Cast<EventLogEntry>()
        .Select(e => e.Source == "yourFilter");
eventLog.Entries .Cast() .选择(e=>e.Source==“yourFilter”);
必须为你的linq查询提供道具!非常适合我的需要!
var eventLog = new EventLog("Application");
eventLog.Entries
        .Cast<EventLogEntry>()
        .Select(e => e.Source == "yourFilter");