CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    58

    [RESOLVED] Help me clean my code up

    I am writing an EventLog filter. Users will be able to enter values for Source, EventID, Category and the message itself. All values would be optional. Anyway, when the EntryWrittenEventHandler is written, I would like to check the entry to see if it meets the parameters written by the user (all of which will be stored in string variables).

    If I was only checking the source, I could use something like
    Code:
    private void EventLog_EntryWritten(object sender, EntryWrittenEventArgs e)
    {
    	string userSource = "MySearchString";
    	
    	if (e.Entry.Source.ToLower() == userSource)
    	{
    		\\ do something
    	}
    }
    But if I want to check more user options, I would need to do something like
    Code:
    private void EventLog_EntryWritten(object sender, EntryWrittenEventArgs e)
    {
    	string userSource = "MySearchString";
    	string userCategory = "MyCategory";
    	string userEventID = "MyEventId";
    	string userMessage = "MyMessage";
    	
    	if (e.Entry.Source == userSource && e.Entry.Category = userCategory etc...)
    	{
    		\\ do something
    	}
    }
    What I'm looking for is some short clever way to AND together all the user options that have been set, but ignore any that have not been set.

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Help me clean my code up

    I don't think you will have much luck trying to shorten it up... to make your event cleaner, you'll type more code to either extend the evententry object to handle comparisons or create a custom class or method to do it... so, it terms of just less code in the one routine on it's own, not much of a chance... sorry.

    (If anyone does come up with something, I'd like to see it though... )

  3. #3
    Join Date
    Apr 2011
    Posts
    58

    Re: Help me clean my code up

    Ooohhh! I think I can answer my own question. I think this is working:
    Code:
    if (e.Entry.Source.Contains(userSource) && e.Entry.Category.Contains(userCategory) && e.Entry.InstanceId.ToString().Contains(userEventId))
    Now, if the users don't want to check for one of the attributes, they just set it to "" and it will always evaluate to true. I think. I hope.
    Last edited by MrGibbage; May 19th, 2011 at 06:55 PM.

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Help me clean my code up

    I did a quick check, and interestingly enough "1234".Contains("") is True ... go figure, I would have guessed the opposite. Well, good on ya, that saves you a little checking for empty options at any rate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured