3

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 + "\">&nbsp;|&nbsp;");
                Response.Write(entry.TimeGenerated.ToString() + "&nbsp;|&nbsp;");
                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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Web-E
  • 1,169
  • 2
  • 12
  • 17

3 Answers3

1
var eventLog = new EventLog("Application");
eventLog.Entries
        .Cast<EventLogEntry>()
        .Select(e => e.Source == "yourFilter");
h-rai
  • 3,636
  • 6
  • 52
  • 76
Andrew Rebane
  • 199
  • 3
  • 12
0

FYI: It's your reverse() that's causing the slowdown. Linq helps with the filtering as long as you don't reverse it afterwards.

I ended up doing this for my purposes:

for(int x = logInfo.Entries.Count-1; x >= 0; x--)
        {
          EventLogEntry entry = (EventLogEntry)logInfo.Entries[x]; ...

To explain a bit more, for every iteration of your loop, you're calling the reverse() which is slow and intensive. You might be able to speed it up if you reverse prior to the foreach loop.

Edmund
  • 1
0

See if these help

Read event log in C#

http://www.vinull.com/Post/2007/10/16/aspnet-working-with-the-event-log.aspx

Community
  • 1
  • 1
Subhash Dike
  • 1,836
  • 1
  • 22
  • 37