I made some changes to the code that you posted and running against a remote
workstation with 1100 entries it takes 1.2 seconds to populate the listview
(100 Mbps, dual 1.2 GHz Athlon).  If I fetch the Message property, the time
goes up to ~ 5 seconds.

One of the things that I notice is that you're calling ToString() on
properties that are already strings and on enums that should be compared
against their native types.  The cost of the method call is not zero, so
skip calling ToString on the Source property.  Also, I would defer fetching
the Message until the user selects the event.  This would mean keeping the
log open (which I'm not doing).

Anyway, hope it helps...

Seang

Here is the reworked code:

private void LoadEventLog()
{
        try
        {
                DateTime dtStart = DateTime.Now;
                using(EventLog log = new EventLog("Application", "david"))
                {
                        // let's get these as strings one time only
                        string sError = EventLogEntryType.Error.ToString();
                        string sWarn = EventLogEntryType.Warning.ToString();
                        string sFailureAudit =
EventLogEntryType.FailureAudit.ToString();
                        string sInfo =
EventLogEntryType.Information.ToString();
                        string sSuccessAudit =
EventLogEntryType.SuccessAudit.ToString();

                        StringBuilder sb = new StringBuilder(128);

                        int nCount = log.Entries.Count;
                        for(int i = 0; i < nCount; i++)
                        {
                                EventLogEntry ele = log.Entries[i];
                                switch(ele.EntryType)
                                {
                                        case EventLogEntryType.Error:
                                                sb.Append(sError);
                                                break;
                                        case EventLogEntryType.Warning:
                                                sb.Append(sWarn);
                                                break;
                                        case EventLogEntryType.FailureAudit:
                                                sb.Append(sFailureAudit);
                                                break;
                                        case EventLogEntryType.Information:
                                                sb.Append(sInfo);
                                                break;
                                        case EventLogEntryType.SuccessAudit:
                                                sb.Append(sSuccessAudit);
                                                break;
                                }

                                sb.AppendFormat("  {0}  {1}   {2}   ({3})
{4}   {5}   {6}",
                                        ele.TimeWritten.ToShortDateString(),

                                        ele.TimeWritten.ToShortTimeString(),

                                        ele.Source, ele.CategoryNumber,
ele.EventID,
                                        ele.UserName, ele.MachineName);

                                ListViewItem lvi = new ListViewItem();
                                lvi.Text = sb.ToString();
                                lvi.Tag = i;
                                m_List.Items.Add(lvi);

                                sb.Length = 0;
                        }
                        TimeSpan ts = DateTime.Now - dtStart;
                        MessageBox.Show(this, string.Format("Time to dump
{0} events = {1} ms.",
                                log.Entries.Count, ts.TotalMilliseconds));

                        log.Close();
                }
        }
        catch(Exception ex)
        {
                Debug.Assert(false, ex.ToString());
        }
}

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to