In my application I want to read local system's application event log. Currently I am using the following code
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 + "\"> | ");
Response.Write(entry.TimeGenerated.ToString() + " | ");
Response.Write(entry.Message.ToString() + "<br>\r\n");
}
}
}
}
I want to show only the log created by ASP.NET.I know I can filter it within the for-each loop.But this takes lot of time as it needs to iterate through the whole application log.Is there any way to to filter it before it goes into any iteration??
====EDIT====
I got a way to make query,don't know whether it really improves the performance
var result = (from EventLogEntry elog in logInfo.Entries
where (elog.Source.ToString().Equals("ASP.NET 2.0.50727.0"))
orderby elog.TimeGenerated descending
select elog).ToList();
and iterate through the result list.