Click here to Skip to main content
Licence 
First Posted 27 Aug 2003
Views 225,054
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
    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  
    GeneralRe: Thanks for the most excellent code! PinmemberMarc Merritt5:32 14 Feb '07  
    Thanks for your feedback, it is very much appreciated. I was a bit of a novice back when I wrote this and have since thought about updating it with many more capabilities. I'm glad you found it useful. Smile | :)
    GeneralUpdate to the code posted above. PinmemberWilliam E. Thompson7:44 14 Feb '07  
    GeneralCOOL!!! PinmemberNicola Costantini0:34 29 Sep '06  
    GeneralRe: COOL!!! PinmemberMarc Merritt16:21 28 Jun '07  
    GeneralA little help with a modification I made to your app.. Pinmembergonepostal5556:31 28 Jun '06  
    GeneralRe: A little help with a modification I made to your app.. PinmemberJim Weiler17:18 7 Sep '07  
    Generalnotification on error event only Pinmemberc-a-b-2:09 9 Mar '06  
    GeneralRe: notification on error event only PinmemberJonnystar10:57 21 Mar '06  
    QuestionMultiple logs? Pinmemberceltboy15:49 30 Oct '05  
    AnswerRe: Multiple logs? PinmemberJonnystar11:01 21 Mar '06  
    QuestionAppropriate Permissions? PinmemberJVMFX3:12 20 Oct '05  
    AnswerRe: Appropriate Permissions? PinmemberMarc Merritt3:37 20 Oct '05  
    GeneralLimitations PinmemberGreg Ennis3:30 22 Dec '04  
    GeneralRe: Limitations Pinsussdm218:27 8 Apr '05  
    GeneralRe: Limitations PinmemberJonnystar11:05 21 Mar '06  
    GeneralProblem with the monitoring tool Pinmemberpat2708816:38 28 Jul '04  
    GeneralRe: Problem with the monitoring tool PinmemberMarc Merritt8:00 28 Jul '04  
    GeneralRe: Problem with the monitoring tool Pinmemberpat2708818:13 28 Jul '04  
    GeneralRe: Problem with the monitoring tool PinmemberMarc Merritt10:06 28 Jul '04  

    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
    Web03 | 2.5.120529.1 | Last Updated 28 Aug 2003
    Article Copyright 2003 by Marc Merritt
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid