.NET Tip: Logging Exceptions to the Event Log Baltimore MD

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

Gp Solutions Inc
410-244-8548
201 N Charles St
Baltimore, MD
Legal Technology Solutions
410-547-8596
36 S Charles St
Baltimore, MD
Bithgroup Inc
410-962-1188
113 W Monument St
Baltimore, MD
Early Morning Software Inc
410-539-0901
227 Holliday St
Baltimore, MD
Affiliated Computer Services Inc
410-347-0794
200 Holliday St
Baltimore, MD
Accessing Cadd
410-296-0888
2545 Wilkens Ave
Baltimore, MD
Hyperion Solutions Corp
410-366-4211
5100 Falls RD
Baltimore, MD
Universal Software Solutions Inc
410-358-8851
6506 Cross Country Blvd
Baltimore, MD
Alherafi System Inc
410-433-7833
5209 York RD
Baltimore, MD
Albert D Valente
410-536-4255
4505 Leeds Ave
Baltimore, MD

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

Gp Solutions Inc

410-244-8548
201 N Charles St
Baltimore, MD

Related Local Events
DC Chamber Technology Series: Session 4
Dates: 12/10/2009 - 12/10/2009
Location: Robert H. Smith School at the Ronald Reagan Building and International Trade Center
Washington, DC
View Details

National Facilities Management & Technology (NFMT)
Dates: 3/16/2010 - 3/28/2010
Location: Baltimore Convention Center
Baltimore, MD
View Details

30th Annual York County Chamber Business & Technology Expo Sneak Preview
Dates: 5/4/2010 - 5/4/2010
Location: Toyota Arena York Expo Center
York, PA
View Details

The 30th Annual York County Chamber Business & Technology Expo presented by DOCEO Office Solutions
Dates: 5/5/2010 - 5/5/2010
Location: Toyota Arena York Expo Center
York, PA
View Details

CSI 2009: The Next Phase In Security
Dates: 10/24/2009 - 10/30/2009
Location: Gaylord National Resort and Convention Center
National Harbor, MD
View Details