.NET Tip: Logging Exceptions to the Event Log Overland Park KS

The system event log is a good place to store information about your .NET application, and writing entries to the event log is fairly straightforward. You can easily add the code for it to a utility class.

Local Companies

Trabon Solutions
(816) 926-9800
420 E Bannister Rd
Kansas City, MO
New Century Computer Solutions
(816) 303-0100
6601 Winchester Ave
Kansas City, MO
Hanmark Inc
816-472-7256
909 Walnut St
Kansas City, MO
System Solutions
(816) 472-9300
601 Walnut St
Kansas City, MO
Beb Software Systems
816-452-4222
1806 Swift Ave
Kansas City, MO
Mersoft Corporation
913-871-6196
9300 W. 110th Street
Overland Park, KS
Amgraf Inc
816-474-4797
1501 Oak St
Kansas City, MO
Cutting Edge Solutions Inc
816-421-2011
819 Walnut St
Kansas City, MO
Cutting Edge Solutions
(816) 421-2011
818 Grand Blvd Ste 900
Kansas City, MO
Drafix Software
816-842-4955
114 W 3rd St
Kansas City, MO

provided by: 
Originally published at Internet.com


Assuming you have Remote Desktop or some other access to a server, the system event log is a good place to store information about your application. I personally prefer to put some information into the database, but errors such as the inability to make a database connection obviously would preclude you from storing an error in the database.

The code to write entries to the event log is fairly straightforward and can easily be added to a base page class or other utility class for ease of use. To configure how the event log writer works, you also can use the configuration file and either a custom configuration section or the standard AppSettings area.

The following code creates three public functions and a private one to do the real work. You easily can read the code to determine which type of entry is being created: public void LogInformation(string message) { LogEntry(message, EventLogEntryType.Information); } public void LogWarning(string message) { LogEntry(message, EventLogEntryType.Warning); } public void LogError(string message) { LogEntry(message, EventLogEntryType.Error); } private void LogEntry(string message, EventLogEntryType logType) { if (!EventLog.SourceExists("NCS.Web")) EventLog.CreateEventSource("NCS.Web", "Application"); EventLog ncsLog = new EventLog(); ncsLog.Source = "NCS.Web"; ncsLog.WriteEntry(message, logType); }

Each call to LogEntry passes a EventLogEntryType constant, which can be one of the three shown or SuccessAudit or FailureAudit. Inside the LogEntry routine, the code first determines whether an event source called NCS.Web (the internal name for the application) has been created. If it hasn't, it creates the event source with a name and location where the log should be stored—whether it should go to the Application, Security, or System log, or a custom log you can create. In this case, the log entries are placed into the Application log, and then an EventLog object is created, the source is set, and the message is written out. You also can specify an event ID and/or a category for the log entry. This makes it easier to sort through the logs when you are diagnosing a problem.

To make this routine more useful, you also could write the events to a database table or to a flat file because it can be somewhat difficult to view the logs from a Web site.

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

Mersoft Corporation

913-871-6196
9300 W. 110th Street
Overland Park, KS
http://www.mersoft.com

Related Local Events
Girls in Science, Technology, Engineering and Mathematics: Exploring Opportunities and Opening Doors
Dates: 10/6/2009 - 10/6/2009
Location: Metropolitan Community College
Kansas City, MO
View Details

BarCampKC
Dates: 9/26/2009 - 9/26/2009
Location: JavaportKC
Kansas City, MO
View Details

Brain Food-Discover the 21st Century Virtual Office
Dates: 9/16/2009 - 9/16/2009
Location: World Trade Center Room
Kansas City, MO
View Details

Brain Food-Discover the 21st Century Virtual Office
Dates: 9/16/2009 - 9/16/2009
Location: World Trade Center Room
Kansas City, MO
View Details