http://www.developer.com/net/cplus/article.php/3453381/Managed-C-Monitoring-the-Windows-Event-Log.htm
This article illustrates how to monitor any event log from a Windows application. You can then take the basics it covers and modify them to your particular environment and needs—such as having the application email you when the security event log records a particular event type.
Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.
Managed C++: Monitoring the Windows Event Log
December 31, 2004
Due to the increasing danger posed by viruses and mal/ad-ware applications, many people have downloaded, purchased, or written their own file and registry monitors. These monitors typically are written to detect malicious software that has been installed—or is being installed—on a machine by prompting warnings when applications attempt to modify the Windows registry or certain files. However, an oft-overlooked area of security is the Windows event log—specifically the security event log, where the Windows operating system and key system applications log events such as invalid logon attempts, port scans, and many other events related to the security of a system.
Monitoring with the .NET EventLog Class
The code in this article uses the .NET 1.0/1.1 Managed C++ syntax. If you're using a newer version of .NET, you'll need to specify the /clr:oldSyntax option in the project's Project Properties dialog box or adjust the code below to conform to the new Managed C++ syntax.
The key .NET type to working with the Windows event log is the Diagnostics::EventLog class.
The handler (OnNewLogEntry) will be called when the "new event-log entry event" is raised. Also note that the event handler must have the same signature as the EntryWrittenEventHandler delegate. Here's an example class:
// Example event handler managed class for
// monitoring new event log entries
__gc class NewLogEntryEventHandler
{
public:
NewLogEntryEventHandler() {}
public:
void OnNewLogEntry(Object* sender,
EntryWrittenEventArgs* e)
{
// Retrieve and work with the newly created entry...
EventLogEntry* entry = e->Entry;
}
};
The EventLog::EnableRaisingEvents property is a boolean type that controls whether or not events are raised after entries are added to the EventLog object's specified log:
EventLog* log = new EventLog("Application");
log->EnableRaisingEvents = true;
NewLogEntryEventHandler* handler = new NewLogEntryEventHandler();
log->EntryWritten +=
new EntryWrittenEventHandler(
handler,
&NewLogEntryEventHandler::OnNewLogEntry);
Pitfalls and Caveats
As you can see, .NET makes subscribing to event log events extremely easy. However, you should also know of a few limitations regarding handling event log notifications:
About the Author