.NET Tip: Searching System Event Logs for Valuable Info Wisconsin

System event logs can provide valuable information, but manually finding the worthwhile details in them can be difficult. Learn the easy, automated ways .NET provides for reading and searching event logs.

Local Companies

Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Advantage Learning System Inc
(715) 424-3636
2911 Peach St
Wisconsin Rapids, WI
Northern Micrographics
(608) 781-0850
2004 Kramer St
La Crosse, WI


.NET Tip: Searching System Event Logs for Valuable Info

provided by: 
Originally published at Internet.com


One of the more tedious tasks a system administrator has to do is review the system event logs using the Event Viewer. These logs can provide valuable information, but manually finding the worthwhile details in them can be difficult. Luckily, .NET provides some easy, automated ways to read and search event logs.

This tip creates a console application that reads a log looking for this type of message: Login failed for user 'sa'. [CLIENT: 255.255.255.255]

The 255.255.255.255 is a network address that is attempting to gain access to SQL Server. These events are logged as Failure Audit events in the Application log, and they seem to come from particular IP addresses. The goal is to detect when one of these occurs and to use the hardware firewall to blacklist the source IP address. However, scanning through the event log to find the addresses is a job better done by the computer.

Although the example application is a console application, you could change it into a service that monitors the log for particular entries on some set interval. You then could send the entry to an administrator via e-mail. Most administrators respond much better reactively than proactively, simply because there are too many things to watch in a large server farm.

The code for an application that reads the event log is simple: using System; using System.Diagnostics; using System.Collections; namespace LogScanner { ///

/// Application to scan system log for a particular message. /// class Executable { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { string address; int startPos, endPos; EventLog appLog = new EventLog("Application"); Hashtable ipAddresses = new Hashtable(); foreach (EventLogEntry e in appLog.Entries) { if (e.Message.IndexOf("Login failed for user 'sa'.") >= 0) { startPos = e.Message.IndexOf("[") + 9; endPos = e.Message.IndexOf("]", startPos); address = e.Message.Substring(startPos, endPos - startPos - 1); if (!ipAddresses.ContainsKey(address.ToString())) { Console.WriteLine("Found " + address + "."); ipAddresses.Add(address.ToString(), address.ToString()); } } } appLog.Close(); } } }

First, the application creates an instance of System.Diagnostics.EventLog to read the built-in Application log. If you've created your own log, you can specify the name of that log as an argument. Next, the program creates a hashtable for the addresses it finds. In my case, I get a whole series of attempts from the same address, but I want only one instance of the address to be displayed. The hashtable lets me quickly store the address and add a new address only if it doesn't match.

The application then loops through the Entries collection of the log and reads the Message property. You also can look at properties such as the error number, the date/time, and so forth to help you find the messages you're looking for. In the case of a service, it would make sense to store the last event entry that your service read and then look for entries only after that time. Otherwise, you'll duplicate your previous results.

Finally, the program looks at the Message property to see if it contains the target message. If so, it extracts the network address (between opening and closing square brackets) and adds it to the hashtable, if it's not already theere. It also dumps out the address to the console so that the user can see the address immediately. The application finishes up by closing the application log object. If you were doing this as a service, you might replace the Console.WriteLine with a block of code at the end that e-mails the administrator the addresses that were found.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

R.E. Coker and Associates, Inc.

262-723-8104
108 W Court St.
Elkhorn, WI
http://www.recoker.com

Regional Articles
- .NET Tip: Searching System Event Logs for Valuable Info Appleton WI
- .NET Tip: Searching System Event Logs for Valuable Info Baraboo WI
- .NET Tip: Searching System Event Logs for Valuable Info Beaver Dam WI
- .NET Tip: Searching System Event Logs for Valuable Info Beloit WI
- .NET Tip: Searching System Event Logs for Valuable Info Brookfield WI
- .NET Tip: Searching System Event Logs for Valuable Info Burlington WI
- .NET Tip: Searching System Event Logs for Valuable Info Cedarburg WI
- .NET Tip: Searching System Event Logs for Valuable Info Chippewa Falls WI
- .NET Tip: Searching System Event Logs for Valuable Info Cudahy WI
- .NET Tip: Searching System Event Logs for Valuable Info De Pere WI
- .NET Tip: Searching System Event Logs for Valuable Info Delavan WI
- .NET Tip: Searching System Event Logs for Valuable Info Eau Claire WI
- .NET Tip: Searching System Event Logs for Valuable Info Elkhorn WI
- .NET Tip: Searching System Event Logs for Valuable Info Fond Du Lac WI
- .NET Tip: Searching System Event Logs for Valuable Info Fort Atkinson WI
- .NET Tip: Searching System Event Logs for Valuable Info Franklin WI
- .NET Tip: Searching System Event Logs for Valuable Info Green Bay WI
- .NET Tip: Searching System Event Logs for Valuable Info Hartland WI
- .NET Tip: Searching System Event Logs for Valuable Info Janesville WI
- .NET Tip: Searching System Event Logs for Valuable Info Kaukauna WI
- .NET Tip: Searching System Event Logs for Valuable Info Kenosha WI
- .NET Tip: Searching System Event Logs for Valuable Info La Crosse WI
- .NET Tip: Searching System Event Logs for Valuable Info Lake Geneva WI
- .NET Tip: Searching System Event Logs for Valuable Info Manitowoc WI
- .NET Tip: Searching System Event Logs for Valuable Info Marinette WI
- .NET Tip: Searching System Event Logs for Valuable Info Marshfield WI
- .NET Tip: Searching System Event Logs for Valuable Info Menasha WI
- .NET Tip: Searching System Event Logs for Valuable Info Menomonee Falls WI
- .NET Tip: Searching System Event Logs for Valuable Info Menomonie WI
- .NET Tip: Searching System Event Logs for Valuable Info Merrill WI
- .NET Tip: Searching System Event Logs for Valuable Info Middleton WI
- .NET Tip: Searching System Event Logs for Valuable Info Milwaukee WI
- .NET Tip: Searching System Event Logs for Valuable Info Mosinee WI
- .NET Tip: Searching System Event Logs for Valuable Info Mukwonago WI
- .NET Tip: Searching System Event Logs for Valuable Info Muskego WI
- .NET Tip: Searching System Event Logs for Valuable Info Neenah WI
- .NET Tip: Searching System Event Logs for Valuable Info New Berlin WI
- .NET Tip: Searching System Event Logs for Valuable Info Oak Creek WI
- .NET Tip: Searching System Event Logs for Valuable Info Oconomowoc WI
- .NET Tip: Searching System Event Logs for Valuable Info Onalaska WI
- .NET Tip: Searching System Event Logs for Valuable Info Oshkosh WI
- .NET Tip: Searching System Event Logs for Valuable Info Pewaukee WI
- .NET Tip: Searching System Event Logs for Valuable Info Racine WI
- .NET Tip: Searching System Event Logs for Valuable Info Rhinelander WI
- .NET Tip: Searching System Event Logs for Valuable Info Rice Lake WI
- .NET Tip: Searching System Event Logs for Valuable Info River Falls WI
- .NET Tip: Searching System Event Logs for Valuable Info Schofield WI
- .NET Tip: Searching System Event Logs for Valuable Info Shawano WI
- .NET Tip: Searching System Event Logs for Valuable Info Sheboygan WI
- .NET Tip: Searching System Event Logs for Valuable Info South Milwaukee WI
- .NET Tip: Searching System Event Logs for Valuable Info Stevens Point WI
- .NET Tip: Searching System Event Logs for Valuable Info Sturgeon Bay WI
- .NET Tip: Searching System Event Logs for Valuable Info Sun Prairie WI
- .NET Tip: Searching System Event Logs for Valuable Info Superior WI
- .NET Tip: Searching System Event Logs for Valuable Info Thiensville WI
- .NET Tip: Searching System Event Logs for Valuable Info Two Rivers WI
- .NET Tip: Searching System Event Logs for Valuable Info Watertown WI
- .NET Tip: Searching System Event Logs for Valuable Info Waukesha WI
- .NET Tip: Searching System Event Logs for Valuable Info Waupaca WI
- .NET Tip: Searching System Event Logs for Valuable Info Wausau WI
- .NET Tip: Searching System Event Logs for Valuable Info West Bend WI
- .NET Tip: Searching System Event Logs for Valuable Info Whitewater WI
- .NET Tip: Searching System Event Logs for Valuable Info Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Industrial Goods & Services Software
Business Services Family Insurance Technology
Career Financial Services Internet Telecommunications
Cars Food & Beverage Legal Transportation & Logistics
Computer Hardware Health Real Estate Travel
Construction Home Services Retail & Consumer Services Wedding
Education