5,666,132 members and growing! (18,549 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

A realtime event log monitoring tool

By Marc Merritt

Demonstrates how to do realtime event log monitoring
C#Windows, .NET, .NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Visual Studio, Dev

Posted: 27 Aug 2003
Updated: 27 Aug 2003
Views: 142,813
Bookmarked: 73 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 5.77 Rating: 4.24 out of 5
1 vote, 4.3%
1
0 votes, 0.0%
2
1 vote, 4.3%
3
7 votes, 30.4%
4
14 votes, 60.9%
5

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


    Doing my own consulting gig these days. It's so fun to work in your pajamas and act professional.

    I live in southeastern Pennsylvania, USA with my lovely wife and two beautiful daughters. Life is good.
    Occupation: Web Developer
    Location: United States United States

    Other popular C# articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 25 of 62 (Total in Forum: 62) (Refresh)FirstPrevNext
    GeneralEvent log monitoring is not supported for remote machinesmemberBruce Ritter8:53 29 Aug '08  
    GeneralCross-thread operation not valid:memberWill Saunders5:05 30 Jul '08  
    QuestionDoesn't work for mememberbitstream030:49 24 Aug '07  
    AnswerRe: Doesn't work for mememberMarc Merritt7:39 24 Aug '07  
    QuestionVistamemberchemelli22:20 6 Jul '07  
    AnswerRe: VistamemberMarc Merritt5:05 8 Jul '07  
    GeneralException trying to recompile and run the event log monitoring source code using Visual Studio 5memberNtanga3:42 27 Jun '07  
    GeneralRe: Exception trying to recompile and run the event log monitoring source code using Visual Studio 5memberMarc Merritt17:19 28 Jun '07  
    GeneralHow to read event full description from remote machine ?memberremotehuang5:49 19 Jun '07  
    GeneralRe: How to read event full description from remote machine ?memberMarc Merritt17:12 28 Jun '07  
    AnswerRe: How to read event full description from remote machine ?memberCool Cassis13:16 6 Jul '07  
    Questioncontact MarcmemberChris Blue20:32 29 Mar '07  
    AnswerRe: contact Marcmemberjbono00713:51 11 May '07  
    AnswerRe: contact MarcmemberMarc Merritt17:21 28 Jun '07  
    Questionwatch all 3 logs + display full log entrymemberChris Blue11:24 10 Aug '07  
    General=== pleeeeease ===memberChris Blue20:09 29 Mar '07  
    GeneralHow to display the EVENT LOG MESSAGE TEXT? 1000 thanks for helping !!!memberChris Blue20:36 2 Mar '07  
    GeneralRe: How to display the EVENT LOG MESSAGE TEXT? 1000 thanks for helping !!!memberChris Blue20:40 2 Mar '07  
    GeneralThanks for the most excellent code!memberWilliam E. Thompson6:18 14 Feb '07  
    GeneralRe: Thanks for the most excellent code!memberMarc Merritt6:32 14 Feb '07  
    GeneralUpdate to the code posted above.memberWilliam E. Thompson8:44 14 Feb '07  
    GeneralCOOL!!!memberNicola Costantini1:34 29 Sep '06  
    GeneralRe: COOL!!!memberMarc Merritt17:21 28 Jun '07  
    GeneralA little help with a modification I made to your app..membergonepostal5557:31 28 Jun '06  
    GeneralRe: A little help with a modification I made to your app..memberJim Weiler18:18 7 Sep '07  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 27 Aug 2003
    Editor: Nishant Sivakumar
    Copyright 2003 by Marc Merritt
    Everything else Copyright © CodeProject, 1999-2008
    Web16 | Advertise on the Code Project