Click here to Skip to main content
Licence 
First Posted 27 Aug 2003
Views 224,916
Bookmarked 97 times

A realtime event log monitoring tool

By | 27 Aug 2003 | Article
Demonstrates how to do realtime event log monitoring

Introduction

I'm an instant gratification kind of person. I like to see who and from where my machine is being accessed, as it occurs. This tool allows you to do just that and provides a number of other event log monitoring capabilities.

Background

While testing a piece of software that provides Windows event logging, our QA team questioned if there was a way to monitor events as they are written to the Windows event log, hence the creation of this little utility. The notifications that the tool displays are done so using the most-excellent NotifyIconEx class by Joel Matthias.

Capturing Events

The EventLog class contains an event handler called EntryWritten. This handler expects an argument of type EntryWrittenEventArgs. To capture events as they happen, we simply set the EnableRaisingEvents property to true and declare the method name that will handle the event.

private void StartWatch()
{      
  EventLog myLog = new EventLog(watchLog);
        
  // set event handler
  myLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
  myLog.EnableRaisingEvents = true;
}

Displaying Events

When events that match the specified criteria occur, a balloon notification is displayed with the details of the last event that was written. (To capture and display Security log events, you must have auditing turned on.)

private void OnEntryWritten(object source, EntryWrittenEventArgs e)
{
  string logName = watchLog;
  GetLogEntryStats(watchLog);
  
  if (logType == eventFilter || eventFilter.Length == 0)
  {
    // show balloon
    NotifyIcon.ShowBalloon("Event Log Monitor",
      "An event was written to the "+logName+" event log."+
      "\nType: "+LogType+
      "\nSource: "+LogSource+
      "\nCategory: "+LogCategory+
      "\nEventID: "+EventID+
      "\nUser: "+User,
      NotifyIconEx.NotifyInfoFlags.Info,
      5000);
        
    LogNotification();
  }
}
    
private void GetLogEntryStats(string logName)
{
  int e = 0;
  
  EventLog log = new EventLog(logName);
  e = log.Entries.Count - 1; // last entry

  logMessage = log.Entries[e].Message;
  logMachine = log.Entries[e].MachineName;
  logSource = log.Entries[e].Source;
  logCategory = log.Entries[e].Category;
  logType = Convert.ToString(log.Entries[e].EntryType);
  eventID = log.Entries[e].EventID.ToString();
  user = log.Entries[e].UserName;
  logTime = log.Entries[e].TimeGenerated.ToShortTimeString();
  log.Close();  // close log
}

The GetEventLogs() method provides an overload for retrieving the logs from a remote machine. It is feasible to assume that event monitoring should work the same on a remote machine as it does on the local computer, given the appropriate permissions. As time permits, I'll be expanding the filtering capabilities of the tool and provide the ability to monitor multiple machines.

Compatibility Issues

The code has only been tested on Windows XP SP1 but should run on Windows 2000. However, while the NotifyIconEx class contains an event handler called BalloonClick, this isn't supported on Windows 2000. Will not work on Win9x or NT4 as they are incapable of displaying balloon notifications.

History

  • Version 1.0 - 08.22.2003

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Marc Merritt

    Technical Lead
    Motorcycle Road Racing Forums
    United States United States

    Member

    Follow on Twitter Follow on Twitter
    I live in southeastern Pennsylvania, USA with my lovely wife and two beautiful daughters. Life is good. My hobbies are motorcycles, motorcycles, and motorcycles.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    QuestionHow to monitor Application and Services Logs/Microsoft/Windows/Diagnosis-PLA PinmemberMember 42454370:05 24 May '12  
    GeneralMy vote of 1 Pinmembermmmgedadads23:09 26 Feb '11  
    GeneralMy vote of 1 Pinmemberblizznet4:54 20 Dec '10  
    GeneralCross-Thread Operation Pinmemberkjward7:18 15 Apr '10  
    GeneralEvent Log Watcher Pinmembertuxplorer23:11 14 Feb '10  
    GeneralEvent log monitoring is not supported for remote machines PinmemberBruce Ritter7:53 29 Aug '08  
    GeneralCross-thread operation not valid: PinmemberWill Saunders4:05 30 Jul '08  
    GeneralRe: Cross-thread operation not valid: Pinmembereyal.flato23:54 8 Dec '08  
    QuestionDoesn't work for me Pinmemberbitstream0323:49 23 Aug '07  
    AnswerRe: Doesn't work for me PinmemberMarc Merritt6:39 24 Aug '07  
    QuestionVista Pinmemberchemelli21:20 6 Jul '07  
    AnswerRe: Vista PinmemberMarc Merritt4:05 8 Jul '07  
    GeneralException trying to recompile and run the event log monitoring source code using Visual Studio 5 PinmemberNtanga2:42 27 Jun '07  
    GeneralRe: Exception trying to recompile and run the event log monitoring source code using Visual Studio 5 PinmemberMarc Merritt16:19 28 Jun '07  
    QuestionHow to read event full description from remote machine ? Pinmemberremotehuang4:49 19 Jun '07  
    AnswerRe: How to read event full description from remote machine ? PinmemberMarc Merritt16:12 28 Jun '07  
    AnswerRe: How to read event full description from remote machine ? PinmemberCool Cassis12:16 6 Jul '07  
    Questioncontact Marc PinmemberChris Blue19:32 29 Mar '07  
    AnswerRe: contact Marc Pinmemberjbono00712:51 11 May '07  
    AnswerRe: contact Marc PinmemberMarc Merritt16:21 28 Jun '07  
    Questionwatch all 3 logs + display full log entry PinmemberChris Blue10:24 10 Aug '07  
    General=== pleeeeease === PinmemberChris Blue19:09 29 Mar '07  
    QuestionHow to display the EVENT LOG MESSAGE TEXT? 1000 thanks for helping !!! PinmemberChris Blue19:36 2 Mar '07  
    AnswerRe: How to display the EVENT LOG MESSAGE TEXT? 1000 thanks for helping !!! PinmemberChris Blue19:40 2 Mar '07  
    GeneralThanks for the most excellent code! PinmemberWilliam E. Thompson5:18 14 Feb '07  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 28 Aug 2003
    Article Copyright 2003 by Marc Merritt
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid