Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:06:02+00:00 2026-05-23T04:06:02+00:00

Im trying to get the details from the event log based on the selection

  • 0

Im trying to get the details from the event log based on the selection of a item in a listbox. I was trying to put the details into a textbox. I havnt been successful in being able to find a solution on my own. What I have done, and is extremely slow, is reiterating through the event log and finding a match to the index of the log, then displaying the message, but that is a time consuming operation. Is there a much faster way to get directly to the specific log entry based off the index of the log. Im using WPF and C#.

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
    EventLog eventLog1 = new EventLog();
    eventLog1.Log = "System";
    foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
    {
        var newEntry = entry.Index + "  -  " + entry.EntryType + " -           " + entry.TimeWritten + "     - " + entry.Source;

        backgroundWorker2.ReportProgress(0, newEntry);
    }
}

void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    var newEntry = (string)e.UserState;

   MainWindow.Instance.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.Instance.listBox1.Items.Add(newEntry); }));
}

And this add each item to the listbox with the index of the item, i then go through and extract the index:

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string p1 = listBox1.SelectedItem.ToString();
    string[] id = Regex.Split(p1, @"([\s])");
    label1.Content = id[0];
    EventLog el = new EventLog();
    el.Log = "System";
    foreach (System.Diagnostics.EventLogEntry entry in el.Entries)
    {
        if (entry.Index.ToString() == id[0])
        {
            label1.Content = entry.Message;
        }

    }
}

The foreach loop is what causes the hang in the UI, but even if I were to set it on a different thread, as I did while adding the items to the listbox, it would still take some time to go through it all to get the exact index Im looking for. So what Im really looking to do is just to go straight through to that index and grab the message instead of iterating through the whole list searching for it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:06:02+00:00Added an answer on May 23, 2026 at 4:06 am

    Upon looking at your code, here’s a quick summary of what I would do:

    Remove ProgressChanged handler. It is intended for reporting current status to user, which you don’t do. Instead, call Items.Add in DoWork handler.

    Create EventLog once. This just seems nicer and saves you from potentially creating it in a loop if you aren’t careful.

    Instead of parsing text with regex, create a special class. This is really important and will save you a lot of pain when you need more accurate behavior or don’t wish to display index at all. Regular expressions are slow, and data intended for display to user must never be parsed. You must use classes.

    Use meaningful names. I know that you didn’t clean up the code, but if you want someone to help you over the internet, you really should.

    Finally, get item by index. If you looked into the documentation, you would have noticed there’s an indexer property that gets item directly by its index.

    class EntryItem {
        public EntryItem (EventLogEntry entry) 
        {
            EntryIndex = entry.Index;
            ItemText = string.Format ("{0} - {1} - {2}     - {3}",
                entry.Index,
                entry.EntryType,
                entry.TimeWritten,
                entry.Source);
        }
    
        public string ItemText { get; private set; }
        public int EntryIndex { get; private set; }
    
        public override string ToString ()
        {
            return ItemText;
        }
    }
    
    private EventLog log = new EventLog {
        Log = "System"
    };
    
    private void eventLoader_DoWork (object sender, DoWorkEventArgs e)
    {
        foreach (EventLogEntry entry in this.log.Entries)
            this.Dispatcher.BeginInvoke (() => eventListBox.Items.Add (new EntryItem (entry)));
    }
    
    private void eventListBox_SelectionChanged (object sender, SelectionChangedEventArgs e)
    {
        EntryItem item = eventListBox.SelectedItem as EntryItem;
        if (item == null)
            return;
    
        var entry = log.Entries [item.EntryIndex];
        currentEntryLabel.Content = entry.Message;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Im trying to get into some basic JavaFX game development and I'm getting confused
Im trying to get a completly data copy from a gridview, itryed clone(), tryed
I'm trying to add some extended error codes to the event log but I
I've been trying to migrate a fairly large web application from IIS6 to IIS7
I am trying to get the emails I send through Gmail from my Ruby
I'm trying to make a gridview that shows details of a table based upon
Trying to get my css / C# functions to look like this: body {
Trying to get an ASP application deployed; it worked for a while but then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.