8 Replies - 439 Views - Last Post: 07 February 2013 - 06:48 AM Rate Topic: -----

#1 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Method to Search Event ID in EventLog

Posted 04 February 2013 - 01:54 AM

Hello Everybody!

I try to construct a method that searches for an EventID in EventLog. If it finds the value it return false, if not it returns true. The problem is the method has an error ..."return result" use of unassigned local variable 'result'. Any ideas how to fix this? Thanks!
public static bool EventLog()
        {
            bool result;
            EventLog log = new EventLog();
            log.Log = "System";
            foreach (EventLogEntry ev in log.Entries)
            {
                if (ev.InstanceId.Equals("7034"))
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
                
            } 
            return result;
        }





Is This A Good Question/Topic? 0
  • +

Replies To: Method to Search Event ID in EventLog

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Method to Search Event ID in EventLog

Posted 04 February 2013 - 02:42 AM

change line 3 to bool result = true, add line break; after line 10 and delete lines 12-15.

The way you have it now it will return 'false' unless the event you are searching for is the very last event.

This post has been edited by Momerath: 04 February 2013 - 02:43 AM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,522
  • Joined: 12-December 12

Re: Method to Search Event ID in EventLog

Posted 04 February 2013 - 03:08 AM

Just as an alternative to the previous post:

        public static bool EventLog() {
            EventLog log = new EventLog();
            log.Log = "System";
            foreach (EventLogEntry ev in log.Entries) {
                if (ev.InstanceId.Equals("7034")) {
                    return true;
                }
            }
            return false;
        }

Was This Post Helpful? 2
  • +
  • -

#4 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Method to Search Event ID in EventLog

Posted 05 February 2013 - 02:32 AM

View Postrockkrose, on 04 February 2013 - 01:54 AM, said:

Hello Everybody!

I try to construct a method that searches for an EventID in EventLog. If it finds the value it return false, if not it returns true. The problem is the method has an error ..."return result" use of unassigned local variable 'result'. Any ideas how to fix this? Thanks!
public static bool EventLog()
        {
            bool result;
            EventLog log = new EventLog();
            log.Log = "System";
            foreach (EventLogEntry ev in log.Entries)
            {
                if (ev.InstanceId.Equals("7034"))
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
                
            } 
            return result;
        }





Hi, Thanks for the reply, I made the changes, the error is fixed but the problem is that the method returns true even if event ID equals 7034, in this case it should return false.

View PostMomerath, on 04 February 2013 - 02:42 AM, said:

change line 3 to bool result = true, add line break; after line 10 and delete lines 12-15.

The way you have it now it will return 'false' unless the event you are searching for is the very last event.


Hi, Thanks for the reply, I made the changes, the error is fixed but the problem is that the method returns true even if event ID equals 7034, in this case it should return false.

View PostMomerath, on 04 February 2013 - 02:42 AM, said:

change line 3 to bool result = true, add line break; after line 10 and delete lines 12-15.

The way you have it now it will return 'false' unless the event you are searching for is the very last event.

Hi, Thanks for the reply, I made the changes, the error is fixed but the problem is that the method returns true even if event ID equals 7034, in this case it should return false.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,522
  • Joined: 12-December 12

Re: Method to Search Event ID in EventLog

Posted 05 February 2013 - 05:06 AM

I'm surprised it works at all!? You have named the function EventLog but this is the name of the object that you are trying to work with:

public static bool EventLog() {
EventLog log = new EventLog();

Use a little more imagination with your names.

Also, you are hard-coding the value "7034", where it would be much more flexible if you supply this number as an argument to your function.
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Method to Search Event ID in EventLog

Posted 05 February 2013 - 06:08 AM

View Postrockkrose, on 04 February 2013 - 01:54 AM, said:

Hi, Thanks for the reply, I made the changes, the error is fixed but the problem is that the method returns true even if event ID equals 7034, in this case it should return false.


That's because of your foreach loop. Imagine that you only have two entries and the first entry is 7034, and the second is 666. On the first iteration through your loop, result is set to false. On the second iteration, result is true. Then the loop ends and then it returns the last value of result which is true. Perhaps you want to break out or return out of your loop instead of continuing on once you find the item you are looking for?

I've been waiting for our LINQ enthusiasts to jump in, but I think they are busy. Look into the Contains() or Any() IEnumerable<T> extension methods.
Was This Post Helpful? 1
  • +
  • -

#7 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Method to Search Event ID in EventLog

Posted 06 February 2013 - 11:17 PM

Thanks a lot for the answers!! This is my final solution and it works good.

public static bool EventLogCrash(string eventID)  
        {  
            EventLog log = new EventLog();  
            log.Log = "System";  
            foreach (EventLogEntry ev in log.Entries)  
            {  
                    if (ev.EventID.ToString().Equals(eventID)) 
                    {
                    Console.WriteLine("Found EventID");
                    return false;  
                    }  
            } 
            Console.WriteLine("EventID not found");
            return true;
       } 




Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Method to Search Event ID in EventLog

Posted 07 February 2013 - 06:37 AM

As an aside, it looks like EventLogEntry.EventID is marked as obsolete in .NET 2.0.
http://msdn.microsof...(v=vs.110).aspx

Anyway, doing the ToString() within the loop is very expensive. You'll want to parse the incoming eventId into an integer, and do an integer to integer comparison. It is expensive because a new string will have be allocated, and code be run to convert an integer into its string representation taking into account the current UI culture for each iteration of your for loop. Additionally, a string compare of a 6 digit number has to compare 28 bytes for each iteration. If you convert the string into an integer outside the loop, you only pay the cost of looking up the UI culture and parsing the string just once. Within the loop, you'll only be comparing 8 bytes.
Was This Post Helpful? 1
  • +
  • -

#9 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Method to Search Event ID in EventLog

Posted 07 February 2013 - 06:48 AM

View PostSkydiver, on 07 February 2013 - 06:37 AM, said:

As an aside, it looks like EventLogEntry.EventID is marked as obsolete in .NET 2.0.
http://msdn.microsof...(v=vs.110).aspx

Anyway, doing the ToString() within the loop is very expensive. You'll want to parse the incoming eventId into an integer, and do an integer to integer comparison. It is expensive because a new string will have be allocated, and code be run to convert an integer into its string representation taking into account the current UI culture for each iteration of your for loop. Additionally, a string compare of a 6 digit number has to compare 28 bytes for each iteration. If you convert the string into an integer outside the loop, you only pay the cost of looking up the UI culture and parsing the string just once. Within the loop, you'll only be comparing 8 bytes.


Yes true, thanks a lot for the advice ! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1